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)