I want to download a file using the URL provided by me at a specific location in Android 12.
So, I used Android Studio to create the application to do this task. The app is simple. When the application is launched it will check if the directory is present or not. If the directory is not present then it will create the folder and download the image using provided URL at a specific folder (/storage/emulated/0/Download/demo) with the help of the download manager.
Below is the code sample:-
public int downloadUrl() {
String url = "https://www.whatsappimages.in/wp-content/uploads/2021/07/Top-HD-sad-quotes-for-whatsapp-status-in-hindi-Pics-Images-Download-Free.gif";/*editText.getText().toString();*/
File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/demo");
if (!direct.exists()) {
direct.mkdirs();
Log.d(TAG, "downloadUrl create folder demo: " + direct.getPath());
}
DownloadManager mgr = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setAllowedNetworkTypes(
DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false)
.setTitle("Demo")
.setDescription("Something useful. No, really.")
.setDestinationInExternalPublicDir("/demo", "test.jpg");
mgr.enqueue(request);
return 0;
}
But not able to download.
Below are the logs:-
---------------------------- PROCESS STARTED (10469) for package com.example.myapplication ----------------------------
2022-10-03 13:30:42.087 10469-10469 Compatibil...geReporter com.example.myapplication D Compat change id reported: 171979766; UID 10146; state: ENABLED
2022-10-03 13:30:42.664 10469-10469 GraphicsEnvironment com.example.myapplication V ANGLE Developer option for 'com.example.myapplication' set to: 'default'
2022-10-03 13:30:42.664 10469-10469 GraphicsEnvironment com.example.myapplication V Neither updatable production driver nor prerelease driver is supported.
2022-10-03 13:30:42.688 10469-10469 NetworkSecurityConfig com.example.myapplication D No Network Security Config specified, using platform default
2022-10-03 13:30:42.694 10469-10469 NetworkSecurityConfig com.example.myapplication D No Network Security Config specified, using platform default
2022-10-03 13:30:42.813 10469-10497 libEGL com.example.myapplication D loaded /vendor/lib64/egl/libEGL_emulation.so
2022-10-03 13:30:42.817 10469-10497 libEGL com.example.myapplication D loaded /vendor/lib64/egl/libGLESv1_CM_emulation.so
2022-10-03 13:30:42.834 10469-10497 libEGL com.example.myapplication D loaded /vendor/lib64/egl/libGLESv2_emulation.so
2022-10-03 13:30:43.425 10469-10469 e.myapplicatio com.example.myapplication W Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (unsupported, reflection, allowed)
2022-10-03 13:30:43.428 10469-10469 e.myapplicatio com.example.myapplication W Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (unsupported, reflection, allowed)
2022-10-03 13:30:43.960 10469-10479 System com.example.myapplication W A resource failed to call close.
2022-10-03 13:30:44.047 10469-10469 demo com.example.myapplication D downloadUrl create folder demo: /storage/emulated/0/Download/demo
2022-10-03 13:30:44.115 10469-10469 AndroidRuntime com.example.myapplication D Shutting down VM
2022-10-03 13:30:44.120 10469-10469 AndroidRuntime com.example.myapplication E FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 10469
java.lang.RuntimeException: Unable to resume activity {com.example.myapplication/com.example.myapplication.MainActivity}: java.lang.IllegalStateException: Not one of standard directories: /demo
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4757)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4790)
at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:54)
at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2210)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7839)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
Caused by: java.lang.IllegalStateException: Not one of standard directories: /demo
at android.os.Parcel.createExceptionOrNull(Parcel.java:2433)
at android.os.Parcel.createException(Parcel.java:2409)
at android.os.Parcel.readException(Parcel.java:2392)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:190)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:142)
at android.content.ContentProviderProxy.call(ContentProviderNative.java:732)
at android.content.ContentProviderClient.call(ContentProviderClient.java:601)
at android.content.ContentProviderClient.call(ContentProviderClient.java:589)
at android.app.DownloadManager$Request.setDestinationInExternalPublicDir(DownloadManager.java:576)
at com.example.myapplication.MainActivity.downloadUrl(MainActivity.java:44)
at com.example.myapplication.MainActivity.onResume(MainActivity.java:64)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1476)
at android.app.Activity.performResume(Activity.java:8191)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4747)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4790)
at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:54)
at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2210)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7839)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
2022-10-03 13:30:44.276 10469-10469 Process com.example.myapplication I Sending signal. PID: 10469 SIG: 9
-------------------
You can see in the above logs that the folder is created but when the download is started the exception is coming.
So I want to create a public directory like Downloads, DCIM, etc in android using the framework.
Can someone please help me with this issue? Thanks in advance.