DownloadManager IllegalStateException 在 DIRECTORY_DOWNLOADS 中创建下载

DownloadManager IllegalStateException creating a download in DIRECTORY_DOWNLOADS

首先,关于这个问题有很多问题,但是none反映了我​​的问题。例如,我读过 this and this.

我遇到的问题是,在 极少数情况下 ,我对 setDestinationInExternalPublicDir 的函数导致以下堆栈跟踪:

Fatal Exception: java.lang.RuntimeException: Unable to start receiver com.onlinetvrecorder.otrapp2.listeners.DownloadUpdateReceiver: java.lang.IllegalStateException: Unable to create directory: /mnt/sdcard/Download
       at android.app.ActivityThread.handleReceiver(ActivityThread.java:2274)
       at android.app.ActivityThread.access00(ActivityThread.java:131)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1272)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loop(Looper.java:137)
       at android.app.ActivityThread.main(ActivityThread.java:4791)
       at java.lang.reflect.Method.invokeNative(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:511)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
       at dalvik.system.NativeStart.main(NativeStart.java)
Caused by java.lang.IllegalStateException: Unable to create directory: /mnt/sdcard/Download
       at android.app.DownloadManager$Request.setDestinationInExternalPublicDir(DownloadManager.java:496)
       at com.myapp.Utils.download(SourceFile:752)
       at com.myapp.Receiver.onReceive(SourceFile:20)
       at android.app.ActivityThread.handleReceiver(ActivityThread.java:2267)
       at android.app.ActivityThread.access00(ActivityThread.java:131)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1272)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loop(Looper.java:137)
       at android.app.ActivityThread.main(ActivityThread.java:4791)
       at java.lang.reflect.Method.invokeNative(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:511)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
       at dalvik.system.NativeStart.main(NativeStart.java)

我正在使用标准 Environment 常量来告诉 DownloadManager 文件保存位置。

android.app.DownloadManager dm = (android.app.DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);

DownloadManager.Request request = new DownloadManager.Request(Uri.parse("url.to.file.ext"));
request.setMimeType("mime/type");

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    Utils.setDownloadRequestVisibility(request, android.app.DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}

request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "file.ext");
request.setTitle(context.getString(R.string.download));

dm.enqueue(request);

如前所述,可能只有不到 1% 的用户遇到此错误。可能是什么原因造成的?

在写问题的过程中,我注意到我可能刚刚自己解决了这个问题。我打算写下有问题的设备型号是什么,但在 Crashlytics 上它显示 "Unknown"。我点击了它,然后显示了以下模型:GI I9500 TMMARS。 运行 Android 4.

TMMARS 似乎是 Trend Micro 的软件。

Trend Micro Mobile App Reputation Service or TMMARS is a cloud-based service that automatically identifies mobile threats based on app behaviors.

系统极有可能没有像应用程序预期的那样运行,原因很简单,因为它是一个威胁检测环境。

崩溃报告示例: