Click here to Skip to main content
15,887,434 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,
I tried following code to download file using download manager.

Java
HWebView.setDownloadListener(new DownloadListener() {
    public void onDownloadStart(String url, String userAgent,
                        String contentDisposition, String mimetype,
                        long contentLength) {
    DownloadManager.Request request = new DownloadManager.Request( Uri.parse(url));
    
    request.allowScanningByMediaScanner();
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    
    request.setDestinationUri(Uri.fromFile(new File(mcontext.getFilesDir() + "/xx.xxx")));
    
    DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
    dm.enqueue(request);
    }
});


I am getting Exception
java.lang.SecurityException: Destination must be on external storage exception.

Thank you in Advance
Posted
Updated 18-Jul-15 2:28am
v3

1 solution

You need to use Environment.getExternalStorageDirectory().getPath(); to get a valid location on external storage.
 
Share this answer
 
Comments
User-arun 18-Jul-15 9:38am    
Thank you Richard.I want to download files to internal memory of mobile not on external storage
Richard MacCutchan 18-Jul-15 9:43am    
So you think that exception message was just a joke?
User-arun 18-Jul-15 9:47am    
No,I am able to save file to external memory using this
request.setDestinationInExternalFilesDir(getApplicationContext(),null,"xx.xxx");
But in the absence of external memory i need to save in internal memory.
Richard MacCutchan 18-Jul-15 9:55am    
No, the exception message is quite clear: your cannot download into internal memory, for security reasons.

I suspect you are confusing the meaning of the two terms.

"internal" memory is really just private space that is for files that can only be accessed by the app that owns them.

"external" memory is the space that is available for all applications to share files, and make them publicly available.
User-arun 18-Jul-15 9:59am    
ok. I dont want to other apps to access downloaded files.Is their any way to achieve this?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900