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

서버를 이용해 파일 다운로드하기 os에 따라 갈립니다..

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 float 형변환 때문에 꼬임.. 그래서 이렇게 만듬..
				float progress_temp = ((float)bytesWritten)/((float)total);
				progress_temp = progress_temp*100;
				int progress = (int)progress_temp;
				mProgressDialog.setProgress(progress);
				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() ,"예", "아니오");
				
			}
		 });
	}

 

파일 다운로드 하는 소스는 위와 같습니다.

문제는 OS 가 롤리팝인 경우에.. 위와 같이 다운로드를 하면.. 정상적이지 않은 APK 파일이 되어서

설치할 때 구문분석오류가 납니다..

기타 다른 OS들은 문제가 없습니다.. 왜 그런지, 어떻게 해결해야되는지 아시는분 계신가요.. 어제 오늘 아주 답답합니다 ㅠㅠ

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

2개의 답변

0 추천
익명사용자 님이 2015년 4월 17일 답변
0 추천
다운로드 구현하실때 라이브러리 안쓰시는 걸 추천드릴게요.

해당 라이브러리가 롤리팝에서 정상적인 동작을 안 할 가능성이 있습니다.

기본 안드로이드 SDK만으로도 충분히 파일 다운로드 구현이 가능하니

라이브러리를 사용안하시는걸 추천드립니다.
익명사용자 님이 2015년 4월 17일 답변
...