3

I have a weird crash in Lollipopand below. I get a security Exception when trying to download a file from the server, but in devices running Marshmallow and above the app does not crash. Logcat:

Caused by: java.lang.SecurityException: need WRITE_EXTERNAL_STORAGE permission to use DESTINATION_FILE_URI: uid 10229 does not have android.permission.WRITE_EXTERNAL_STORAGE.

In App's grandle I use

 targetSdkVersion 22

Will change after our first push on Google Console to target the latest sdk version, but for now this will remain to 22, so no need for run time permissions. Furthermore, in App's Manifest we declare app's permission.

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

Also, the code that uses the DownloadManagerinside an AsynckTask :

try {
        String dirPath = String.format("%5$s/%1$s/Resources/%2$s/%3$s/%4$s/", userID, resource.getiD(), filePackage.getiD(), language, dir.getCanonicalPath());
        File makeDirs = new File(dirPath);
        makeDirs.mkdirs();
    } catch (Exception ex) {
        ex.printStackTrace();
}

this.downloadManager = (DownloadManager) getApplicationContext().getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(url);
        request.setDestinationInExternalFilesDir(getApplicationContext(), null, filePath);
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
        request.setVisibleInDownloadsUi(false);
        downloadID = downloadManager.enqueue(request);

Again, this crash is observed only in Galaxy A5 with OS version 5.0.2, in an emulator running Samsung Galaxy S6 with Lollipop and in emulator running Kitkat 4.4.4 HTC One.

The full stack trace :

    java.lang.RuntimeException: Unable to start activity ComponentInfo{...players.PDFViewer}: java.lang.SecurityException: need WRITE_EXTERNAL_STORAGE permission to use DESTINATION_FILE_URI: uid 10229 does not have android.permission.WRITE_EXTERNAL_STORAGE.
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2808)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2873)
                                                                             at android.app.ActivityThread.access$900(ActivityThread.java:181)
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1482)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                             at android.os.Looper.loop(Looper.java:145)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:6145)
                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                             at java.lang.reflect.Method.invoke(Method.java:372)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
                                                                          Caused by: java.lang.SecurityException: need WRITE_EXTERNAL_STORAGE permission to use DESTINATION_FILE_URI: uid 10229 does not have android.permission.WRITE_EXTERNAL_STORAGE.
                                                                             at android.os.Parcel.readException(Parcel.java:1540)
                                                                             at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:185)
                                                                             at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
                                                                             at android.content.ContentProviderProxy.insert(ContentProviderNative.java:475)
                                                                             at android.content.ContentResolver.insert(ContentResolver.java:1260)
                                                                             at android.app.DownloadManager.enqueue(DownloadManager.java:1336)
                                                                             at de.imsystems.crmmobile.players.PDFViewer.onCreate(PDFViewer.java:104)
                                                                             at android.app.Activity.performCreate(Activity.java:6374)
                                                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2752)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2873) 
                                                                             at android.app.ActivityThread.access$900(ActivityThread.java:181) 
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1482) 
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                             at android.os.Looper.loop(Looper.java:145) 
                                                                             at android.app.ActivityThread.main(ActivityThread.java:6145) 
                                                                             at java.lang.reflect.Method.invoke(Native Method) 
                                                                             at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) 
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 

If you have any thoughts, please share them, thanks in advance.

VasFou
  • 1,561
  • 1
  • 22
  • 39
  • `filePath`. Value please. – greenapps Oct 31 '17 at 12:19
  • `at android.os.Parcel.readException(Parcel.java:1540)`. You could add request for READ_EXTERNAL_STORAGE. – greenapps Oct 31 '17 at 12:21
  • `the code that uses the DownloadManagerinside an AsynckTask :`. You should not put that code in an async task. – greenapps Oct 31 '17 at 12:23
  • @greenapps filepath: "44490/Resources/5247/6337/en-GB/Training.pdf" , why I should not put the code on `AsyncTask` ? – VasFou Oct 31 '17 at 13:58
  • @greenapps dir= "/storage/emulated/0/Android/data/development.standard/files" – VasFou Oct 31 '17 at 14:00
  • So the directory where you want to put the pdf file in is `/storage/emulated/0/Android/data/development.standard/files‌/44490/Resources/5247/6337/en-GB`. Does that directory exist? – greenapps Oct 31 '17 at 14:04
  • @greenapps Before creating Download.Request , a dir is created : I will update the question – VasFou Oct 31 '17 at 14:21
  • `makeDirs.mkdirs();` Two times wrong! You should only call mkdirs() when the directory does not exist. Further you should check the return value of mkdirs() as it might fail to create the directory. In which case you should display a toast to the user saying so and return. Do not continue then as it makes little sense to download a file to a directory that does not exist. – greenapps Oct 31 '17 at 14:28
  • `String dirPath = String.format("%5$s/%1$s/Resources/%2$s/%3$s/%4$s/", userID,` Objection! Who on earth can see what path that would be? You are supposted to post code that is reproducable and understandable! – greenapps Oct 31 '17 at 14:33
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/157918/discussion-between-vasilisfoo-and-greenapps). – VasFou Oct 31 '17 at 14:37

1 Answers1

1

Eventually this post WRITE_EXTERNAL_STORAGE not working on lollipop even though it's set in the manifest helped me. What I did was to alter permissions in AndroidManifest.xml file like this:

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="26" tools:replace="android:maxSdkVersion"/>

This way the app doesn't crashes!

VasFou
  • 1,561
  • 1
  • 22
  • 39