Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How store Downloaded file using DownloadManager to app's files directory allocated?

I am trying to store downloaded file using DownloadManager to app/s files directory allocated by android.For this i am using following code

downloadManager=(DownloadManager)getSystemService(Context.DOWNLOAD_SERVICE);
    Uri uri = Uri.parse(link);
    DownloadManager.Request request= new DownloadManager.Request(uri);
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    request.setVisibleInDownloadsUi(false);
    request.setTitle(fileName);

    File file = ctx.getFilesDir();
    Uri myPath=Uri.fromFile(file);
    request.setDestinationUri(myPath);

    Long reference= downloadManager.enqueue(request);
    Toast.makeText(DownloadActivity.this,"Downloading Started",Toast.LENGTH_LONG).show();

By using this code i am getting following error

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.puranepaper.PuranePaper, PID: 18263
java.lang.SecurityException: Unsupported path /data/data/com.puranepaper.PuranePaper/files
    at android.os.Parcel.readException(Parcel.java:2005)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
    at android.content.ContentProviderProxy.insert(ContentProviderNative.java:476)
    at android.content.ContentResolver.insert(ContentResolver.java:1552)
    at android.app.DownloadManager.enqueue(DownloadManager.java:1163)
    at com.puranepaper.PuranePaper.DownloadActivity$1.onClick(DownloadActivity.java:92)
    at android.view.View.performClick(View.java:6310)
    at android.view.View$PerformClick.run(View.java:24970)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:171)
    at android.app.ActivityThread.main(ActivityThread.java:6654)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)

How i can remove "Unsupported path" error and what i am doing wrong in this code

like image 747
sunil Avatar asked Nov 09 '18 17:11

sunil


People also ask

How do I find files downloaded by download manager?

Open the Files app, then tap the Downloads category. By default, it shows you every file you've downloaded in the All tab. Tap the Download tab to only see the files in that specific folder.

What is the use of download manager in Android?

“ The download manager is a system service that handles long-running HTTP downloads. Clients may request that a URI be downloaded to a particular destination file.”


1 Answers

Other apps (including the download manager) have no access to your app's internal memory.

Duplicate of this question.

like image 79
Blehi Avatar answered Sep 20 '22 20:09

Blehi