2

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?

John Roberts
  • 5,885
  • 21
  • 70
  • 124
  • Using an emulator to test? – AndyFaizan Apr 27 '14 at 15:34
  • A (pretty silly) workaround [here](http://stackoverflow.com/questions/13056109/android-runtime-error-unable-to-create-directory-on-use-of-class-downloadman) – AndyFaizan Apr 27 '14 at 15:35
  • @AndyFaizan No, I am using a phone. I took a look at that link earlier, but I couldn't get a confident grasp on how to apply it to my code. Could you help me out with that? – John Roberts Apr 27 '14 at 15:40
  • Did you try this `String name = Environment.getExternalStorageDirectory().getAbsolutePath(); name += "/YourDirectoryName/" ; request.setDestinationInExternalPublicDir(name, segments.get(segments.size()-1));` – AndyFaizan Apr 27 '14 at 16:06
  • What would "/YourDirectoryName/" be in my case? Also, do you have any idea what actually causes this error? Is it a lack of space on the SD card? – John Roberts Apr 27 '14 at 22:24
  • You could name it MyDownloads. Although frankly speaking I'm not familiar with the API. I saw the documentation and based on the answers, I've given suggestions. I haven't yet figured out the exact problem. – AndyFaizan Apr 28 '14 at 19:00

1 Answers1

-1

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.

r_19
  • 1,858
  • 1
  • 20
  • 16