简体   繁体   中英

java.lang.IllegalStateException: Unable to create directory: /mnt/sdcard/Download

I recently saw the following crash on the Developer Console for my app:

java.lang.RuntimeException: Unable to start activity ComponentInfo{}: java.lang.IllegalStateException: Unable to create directory: /mnt/sdcard/Download
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
at android.app.ActivityThread.access$600(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:812)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:579)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: Unable to create directory: /mnt/sdcard/Download
at android.app.DownloadManager$Request.setDestinationInExternalPublicDir(DownloadManager.java:492)
at com...BrowserIntent.a(Unknown Source)
at com...BrowserIntent.onCreate(Unknown Source)
at android.app.Activity.performCreate(Activity.java:4465)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
... 11 more

Here is the corresponding code:

final Intent intent = getIntent();
final List<String> segments = intent.getData().getPathSegments();
String url = intent.getDataString();
if (!url.startsWith("https://") && !url.startsWith("http://")){
    url = "http://" + url;
}
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));              
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, segments.get(segments.size()-1));

Here is part of my manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package=""
    android:versionCode="10"
    android:versionName="1.9" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="19" />

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ad_icon"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Holo.NoActionBar" >

Anyone have any idea what might be causing this?

Try restarting your device. This seems a stupid solution but it solved this problem in my app in my nexus device.

What happened to me when I was testing my app before was, it is working/downloading the file then i'll delete it then redownload it for several times, then suddenly it would crash and throw this error saying that unable to create directory but has been creating/using that already.

this is how I use it in my code

public static String downloadPath = "/My-Folder-Name";
File fileDir = new File(downloadPath);

if (fileDir.isDirectory()) {
        request.setDestinationInExternalPublicDir(downloadPath, filename);
    } else {
        fileDir.mkdirs();
        request.setDestinationInExternalPublicDir(downloadPath, filename);
    }

I'm not sure if this bug is because the SSD storage of the device is failing that's why when I restart the device it works again, then would throw an error again after many times of downloading or when the device hasn't restarted for a while.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM