0

I am able to successfully download on the emulator but the app crashes when I try it on a real device. This is my download image code, it should be working. This is in my adapter class.

     DownloadManager dm = (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE);
            Uri downloadUri = Uri.parse(mImageUrls.get(position));
            DownloadManager.Request request = new DownloadManager.Request(downloadUri);
            request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | 
     DownloadManager.Request.NETWORK_MOBILE)
           .setAllowedOverRoaming(false)
           .setTitle(mNames.get(position))
           .setMimeType("image/jpeg")
           .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
           .setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES,
                    File.separator + DIR_NAME + File.separator + mNames.get(position));

     dm.enqueue(request);
     Toast.makeText(mContext, "Downloading file", Toast.LENGTH_SHORT).show();

I think it crashes because real devices have app permissions to enable download into storage. However how would I do that? If that is not the case, could someone lead me in the right path.

Edit: Here is the error log. It indeed says I need permissions but when I add WRITE_EXTERNAL_STORAGE to Manifest, it still crashes

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.animesearcher, PID: 28292
java.lang.SecurityException: No permission to write to /storage/BF53-91C1/Pictures/App/image: Neither user 10241 nor current process has android.permission.WRITE_EXTERNAL_STORAGE.
    at android.os.Parcel.readException(Parcel.java:1954)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
    at android.content.ContentProviderProxy.insert(ContentProviderNative.java:476)
    at android.content.ContentResolver.insert(ContentResolver.java:1557)
    at android.app.DownloadManager.enqueue(DownloadManager.java:1042)
    at pinterest.StaggeredRecyclerViewAdapter$1.onClick(StaggeredRecyclerViewAdapter.java:128)
    at android.view.View.performClick(View.java:6291)
    at android.view.View$PerformClick.run(View.java:24931)
    at android.os.Handler.handleCallback(Handler.java:808)
    at android.os.Handler.dispatchMessage(Handler.java:101)
    at android.os.Looper.loop(Looper.java:166)
    at android.app.ActivityThread.main(ActivityThread.java:7529)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
sbot
  • 1
  • 2
  • _"the app crashes"_ You could have a look at [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) and then add the crash log to the question. It will tell the exact type and code line of the crash. – Markus Kauppinen Aug 27 '20 at 06:31
  • One thing to check is permissions and API levels. If you are using an API level which is higher than your mobile phone without protection it may crash. Or if you don't have permission to do something and do not handle it, it may also crash. Another recommendation could be to uninstall and reinstall your app, do a clean and rebuild. – Thomas Morris Aug 27 '20 at 07:19
  • @MarkusKauppinen I have updated the error logs and added External storage edit to manifests – sbot Aug 27 '20 at 19:12
  • In modern Android versions many permission need to be [asked for at runtime](https://developer.android.com/training/permissions/requesting) and it's not enough to just declare them in the manifest. – Markus Kauppinen Aug 28 '20 at 06:36

0 Answers0