0

i'm using the DownloadManager to download files and save in a folder inside downloads folder in my application context folder on internal storage, this works fine in Android 6 but in android 4 the path returns null. This code works in android 6 but not in android 4

File p = getApplicationContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);

i have change to this and works

File p = getApplicationContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
if(p == null){
    p = getApplicationContext().getDir(Environment.DIRECTORY_DOWNLOADS, MODE_PRIVATE);
}

My problem is in DownloadManager, when i use the follow code an NullPointerReference occurs

req.setDestinationInExternalFilesDir(getApplicationContext(), Environment.DIRECTORY_DOWNLOADS + "/viewer", fileName);

can anyone help me?

EDIT

This is the stack trace

FATAL EXCEPTION: main
java.lang.NullPointerException: file
at android.net.Uri.fromFile(Uri.java:441)
at android.app.DownloadManager$Request.setDestinationFromBase(DownloadManager.java:508)
at android.app.DownloadManager$Request.setDestinationInExternalFilesDir(DownloadManager.java:470)
at br.com.digitaldoc.ddv4.activities.MainActivity$7.onClick(MainActivity.java:405)
at android.support.v7.app.AlertController$AlertParams$3.onItemClick(AlertController.java:958)
at android.widget.AdapterView.performItemClick(AdapterView.java:298)
at android.widget.AbsListView.performItemClick(AbsListView.java:1088)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:2861)
at android.widget.AbsListView$1.run(AbsListView.java:3535)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4931)
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:791)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
at dalvik.system.NativeStart.main(Native Method)

and this line MainActivity.java:405 is

req.setDestinationInExternalFilesDir(getApplicationContext(), Environment.DIRECTORY_DOWNLOADS + "/viewer", fileName);
Alexandre Heinen
  • 623
  • 1
  • 7
  • 19
  • 2
    `DIRECTORY_DOWNLOADS` is not one of [the documented values for `getExternalFilesDir()`](http://developer.android.com/reference/android/content/Context.html#getExternalFilesDir%28java.lang.String%29), so you may encounter problems with that. Considering using one of the other ones, or simply `null`. – CommonsWare Apr 14 '16 at 19:46
  • thanks for your answer, my main problem is in the DownloadManager in setDestinationInExternalFilesDir, i have tried to set another values and already have a NullPointerException. There are another way to set the path or other solution to this? – Alexandre Heinen Apr 14 '16 at 19:56
  • Please post the Java stack trace associated with your `NullPointerException`. – CommonsWare Apr 14 '16 at 19:58
  • req not is null. I have edited the question with the stack trace. – Alexandre Heinen Apr 14 '16 at 20:15

0 Answers0