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의 값이 변경되지도 않습니다... 이유가 뭘까요...
혹시 용량의 제한이 있는가요? 용량이 조금 적은 파일을 했을 경우에는 정상적으로 되더니.. 용량이 커지니 또 에러가 발생하네요..