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

파일이 있는데 없다고 나옵니다.

0 추천

서버에서 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를 리턴합니다.;

분명히 경로에는 파일이 있는데 왜 없다고 나오는지; 이해가 안됩니다.

뭔가 잘못한 부분이 있나요 ?

 

 

 

now882002 (3,860 포인트) 님이 2018년 1월 16일 질문

1개의 답변

0 추천
 
채택된 답변
installApk()이 호출되는 시점이 Download request가 완료된 후가 아니고, Download request를

queue에 넣어놓은 상태이네요. 아직 Download가 시작되거나 완료되지 않았으니 파일이 존재하지 않겠죠.
디자이너정 (42,810 포인트) 님이 2018년 1월 16일 답변
now882002님이 2018년 1월 16일 채택됨
dm.enqueue(request);에서 뭔가 더해줘야하나요 ??
아니면 다운로드가 끝나는시점은 어떻게 알수있나요 ?
감사합니다.
IntentFilter completeFilter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
        registerReceiver(completeReceiver, completeFilter);
리시버를 달아야하는군요
...