서버에서 apk 를 다운로드 받은후
설치를 진행하려고 합니다.
loginview.loadUrl(url);
loginview.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
File apkFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)+"/aquan.apk");
if(apkFile.exists()){
apkFile.delete();
}
Log.i(TAG,"contentDisposition:"+contentDisposition+" mimetype:"+mimetype+" contentLength:"+contentLength);
//Intent i = new Intent(Intent.ACTION_VIEW);
//i.setData(Uri.parse(url));
//startActivity(i);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setMimeType(mimetype);
request.addRequestHeader("User-Agent", userAgent);
request.setDescription("Downloading file");
String fileName = contentDisposition.replace(contentDisposition, "aquan.apk");
request.setTitle(fileName);
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
installApk();
}
});
이렇게 받는거 까지 완료 했고 해당경로에 가면
/storage/sdcard/Download/aquan.apk가 잘들어있습니다.
그런데 install 를 하려고
private void installApk(){
Log.i(TAG,"설치 1");
// 안드로이드 패키지 매니저를 통해 다운 받은 apk 파일을 처리하도록 한다.
File apkFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)+"/aquan.apk");
if(!apkFile.exists()){
Log.i(TAG,"!exists "+apkFile.getPath());
//return;
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType( Uri.fromFile(apkFile), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Log.i(TAG,"설치 ");
finish();
}
파일 체크를 하니 존재하지 않는 파일이 라고 false를 리턴합니다.;
분명히 경로에는 파일이 있는데 왜 없다고 나오는지; 이해가 안됩니다.
뭔가 잘못한 부분이 있나요 ?