Firstly, there are a lot of questions on this subject, but none reflect my issue. I have for example read this and this.
The issue that I have, is that in an extremely small number of cases, my function to setDestinationInExternalPublicDir
results in the following stack trace:
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.access$1500(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.access$1500(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)
I am using a standard Environment
constant to tell the DownloadManager
where to save the file.
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);
As previously stated, there are perhaps not even 1% of users getting this error. What could be causing it?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…