마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

FileAsyncHttpResponseHandler 로 파일 다운로드 할 때 onProcess가 이상합니다.

0 추천
private void apkInstall() {
		 String url = CommonUtil.Server.getURL_DOWNLOAD();
		 
		 final ProgressDialog mProgressDialog = new ProgressDialog(this);
		 mProgressDialog.setMessage(ErrorCode.Ing_Download.getMessage_Wait());
		 mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		 mProgressDialog.setCancelable(false);
		 mProgressDialog.show();
	  		
		 AsyncHttpClient client = new AsyncHttpClient();
		 //client.setURLEncodingEnabled(false);
		 client.get(url, new FileAsyncHttpResponseHandler(this) {
			int total = CommonUtil.APK_TOTAL_SIZE;
			
			@Override
			public void onProgress(int bytesWritten, int totalSize) {
		    	 int progress_temp = (int) bytesWritten * 100 / total;
		    	 mProgressDialog.setProgress(progress_temp);
		    	 super.onProgress(bytesWritten, totalSize);
			}

			@Override
		     public void onSuccess(int statusCode, Header[] headers, File response) {
		    	 mProgressDialog.dismiss();
				try {
					File SDCardRoot = Environment.getExternalStorageDirectory();   
				    File file = new File(SDCardRoot,"SBBMobile.apk");  
				    FileOutputStream fileOutput = new FileOutputStream(file);
				    Log.e("DOWNLOAD", "fileoutput");
					
				    FileInputStream fin = new FileInputStream(response);
				    
				    byte[] buffer = new byte[1024];   
				    int bufferLength = 0; 

				    while ( (bufferLength = fin.read(buffer)) > 0 )  {   
				    	fileOutput.write(buffer, 0, bufferLength);  
				    }

				    fin.close(); 
				    fileOutput.close(); 
				    
			    	//Log.i(LOG_TAG, response.getAbsolutePath() + file.getAbsolutePath() );	
			    	
			    	Intent intent2 = new Intent(Intent.ACTION_VIEW);
					intent2.setDataAndType( Uri.fromFile(file), "application/vnd.android.package-archive");
					startActivity(intent2);

				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				} 
		     }

			@Override
			public void onFailure(int arg0, Header[] arg1, Throwable arg2, File arg3) {
				mProgressDialog.dismiss();
				
				showAlertDialog(ErrorCode.FAIL_Download.getCode() ,ErrorCode.FAIL_Download.getMessage_Restart() ,"예", "아니오");
				
			}
		 });
	}

먼저 소스는 위와 같구요..

onProgress 의 파라미터로 쓰이는 totalSize는  -1만 받길래.. 총 파일사이즈는 따로 구해와서 변수 설정 해뒀습니다.

문제는..

public void onProgress(int bytesWritten, int totalSize) {
		    	 int progress_temp = (int) bytesWritten * 100 / total;
		    	 mProgressDialog.setProgress(progress_temp);
		    	 super.onProgress(bytesWritten, totalSize);
			}

인데요.. bytesWritten 은 계속 증가하고 있고.. total 변수값은 파일의 총 사이즈로 고정인데..

얘가 70만 되면.. 갑자기 -70으로 전환되던.. -69 -68.. 그렇다고 bytesWritten의 값인 줄어들거나 total의 값이 변경되지도 않습니다... 이유가 뭘까요...

혹시 용량의 제한이 있는가요? 용량이 조금 적은 파일을 했을 경우에는 정상적으로 되더니.. 용량이 커지니 또 에러가 발생하네요..

풍관 (650 포인트) 님이 2015년 4월 16일 질문
풍관님이 2015년 4월 16일 수정

1개의 답변

0 추천

자문 자답입니다.

수식에 문제가 있었나 봅니다. int float 형변환 간에 문제가 발생한듯.. 자세한건 모르겠습니다만.. int 의 양수 범위를 벗어나서

음수로 나온것 같습니다.

 
float progress_temp = ((float)bytesWritten)/((float)total);
progress_temp = progress_temp*100;
int progress = (int)progress_temp;
mProgressDialog.setProgress(progress);
super.onProgress(bytesWritten, totalSize);
 
onProgress 부분을 아래와 같이 변경하였습니다.
풍관 (650 포인트) 님이 2015년 4월 16일 답변
...