如何使用DownloadManager将下载的文件存储到分配给应用程序的文件目录中?

时间:2018-11-09 17:09:25

标签: android download directory store

我正在尝试使用DownloadManager将下载的文件存储到android分配的app / s文件目录中。为此,我正在使用以下代码

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();

通过使用此代码,我得到以下错误

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)

如何清除“不受支持的路径”错误以及此代码中我在做什么错误

2 个答案:

答案 0 :(得分:1)

其他应用程序(包括下载管理器)无权访问您应用程序的内存。

this question重复。

答案 1 :(得分:0)

使用“ file://”启动myPath

request.setDestinationUri(“ file://” + myPath);

这对我有用:

public void downloadFile(String address, String destination) {  
        ///// Baja el archivo a la carpeta indicada.
        Uri uri = Uri.parse(address);
        String Nombre = address.substring(address.lastIndexOf('/') + 1);

        DownloadManager.Request request = new DownloadManager.Request(uri);
        // request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI); // Solo WiFi.
           request.setDestinationUri(Uri.parse("file://" + Environment.getExternalStorageDirectory() + destination  + "/"  + Nombre));
        // Bajada.
        DownloadManager downloadmanager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
        downloadmanager.enqueue(request);
    }

downloadFile(“ http://example.com/myfile.jpg”,“ / mydirectory”)