如何在真实设备上下载

问题描述

我能够在模拟器上成功下载,但是在真实设备上尝试运行该应用程序时会崩溃。 这是我的下载图像代码,应该可以正常工作。这在我的适配器类中。

     DownloadManager dm = (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE);
            Uri downloadUri = Uri.parse(mImageUrls.get(position));
            DownloadManager.Request request = new DownloadManager.Request(downloadUri);
            request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | 
     DownloadManager.Request.NETWORK_MOBILE)
           .setAllowedOverRoaming(false)
           .setTitle(mNames.get(position))
           .setMimeType("image/jpeg")
           .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
           .setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES,File.separator + DIR_NAME + File.separator + mNames.get(position));

     dm.enqueue(request);
     Toast.makeText(mContext,"Downloading file",Toast.LENGTH_SHORT).show();

我认为它崩溃是因为实际设备具有应用程序许可权,以允许下载到存储。但是我该怎么做?如果不是这种情况,有人可以带领我走上正确的道路。

编辑:这是错误日志。它确实说我需要权限,但是当我向清单添加WRITE_EXTERNAL_STORAGE时,它仍然崩溃

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.animesearcher,PID: 28292
java.lang.SecurityException: No permission to write to /storage/BF53-91C1/Pictures/App/image: Neither user 10241 nor current process has android.permission.WRITE_EXTERNAL_STORAGE.
    at android.os.Parcel.readException(Parcel.java:1954)
    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:1557)
    at android.app.DownloadManager.enqueue(DownloadManager.java:1042)
    at pinterest.StaggeredRecyclerViewAdapter$1.onClick(StaggeredRecyclerViewAdapter.java:128)
    at android.view.View.performClick(View.java:6291)
    at android.view.View$PerformClick.run(View.java:24931)
    at android.os.Handler.handleCallback(Handler.java:808)
    at android.os.Handler.dispatchMessage(Handler.java:101)
    at android.os.Looper.loop(Looper.java:166)
    at android.app.ActivityThread.main(ActivityThread.java:7529)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...