Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to create external files directory - ANDROID

Tags:

file

android

Here is my code and the error below. I get an error in the logs saying Unable to create external files directory.

String downloadURL = getString(R.string.download_URL);
Uri uri = Uri.parse(downloadURL);
DownloadManager.Request request = new Request(uri);
request.setAllowedNetworkTypes(Request.NETWORK_WIFI | Request.NETWORK_MOBILE);
request.setAllowedOverRoaming(false);
request.setTitle("Android Jelly Bean's Pic Download");
request.setDescription("Android Jelly Beans Pic Download using Download Manager");
request.setDestinationInExternalFilesDir(getApplicationContext(),    
Environment.DIRECTORY_DOWNLOADS, "abc.png");
downloadReference = dm.enqueue(request);

The line which has the error is request.setDestinationInExternalFilesDir(getApplicationContext(),
Environment.DIRECTORY_DOWNLOADS, "abc.png");

12-05 18:51:55.436: W/ApplicationContext(1049): Unable to create external files directory
12-05 18:51:55.436: D/AndroidRuntime(1049): Shutting down VM
12-05 18:51:55.446: W/dalvikvm(1049): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
12-05 18:51:55.476: E/AndroidRuntime(1049): FATAL EXCEPTION: main
12-05 18:51:55.476: E/AndroidRuntime(1049): java.lang.NullPointerException: file
12-05 18:51:55.476: E/AndroidRuntime(1049):     at android.net.Uri.fromFile(Uri.java:441)
12-05 18:51:55.476: E/AndroidRuntime(1049):     at android.app.DownloadManager$Request.setDestinationFromBase(DownloadManager.java:504)
12-05 18:51:55.476: E/AndroidRuntime(1049):     at android.app.DownloadManager$Request.setDestinationInExternalFilesDir(DownloadManager.java:466)
12-05 18:51:55.476: E/AndroidRuntime(1049):     at com.example.downloadmanagerapplication.DownloadManagerActivity.startDownload(DownloadManagerActivity.java:93)
12-05 18:51:55.476: E/AndroidRuntime(1049):     at com.example.downloadmanagerapplication.DownloadManagerActivity$1.onClick(DownloadManagerActivity.java:115)
12-05 18:51:55.476: E/AndroidRuntime(1049):     at android.view.View.performClick(View.java:3480)
12-05 18:51:55.476: E/AndroidRuntime(1049):     at android.view.View$PerformClick.run(View.java:13983)
12-05 18:51:55.476: E/AndroidRuntime(1049):     at android.os.Handler.handleCallback(Handler.java:605)
12-05 18:51:55.476: E/AndroidRuntime(1049):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-05 18:51:55.476: E/AndroidRuntime(1049):     at android.os.Looper.loop(Looper.java:137)
12-05 18:51:55.476: E/AndroidRuntime(1049):     at android.app.ActivityThread.main(ActivityThread.java:4340)
12-05 18:51:55.476: E/AndroidRuntime(1049):     at java.lang.reflect.Method.invokeNative(Native Method)
12-05 18:51:55.476: E/AndroidRuntime(1049):     at java.lang.reflect.Method.invoke(Method.java:511)
12-05 18:51:55.476: E/AndroidRuntime(1049):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-05 18:51:55.476: E/AndroidRuntime(1049):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-05 18:51:55.476: E/AndroidRuntime(1049):     at dalvik.system.NativeStart.main(Native Method)
like image 566
Tameem Ahmed Avatar asked Dec 20 '22 12:12

Tameem Ahmed


1 Answers

In order to save/write the data into file or create/remove directories, you must have to set "usage" permission in manifest file.

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

Sample : Android DownloadManager

like image 176
KV Prajapati Avatar answered Dec 29 '22 10:12

KV Prajapati