0

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.

HoRn
  • 1,458
  • 5
  • 20
  • 25
Teapot
  • 1
  • 2
  • android 12 does not support direct access to any public folder. You can use `contentResolver` or `SAF API` to access these public folders. – M DEV Oct 03 '22 at 09:48
  • I can access the public directories and download content in it but my task is to create a new public directory in android 12 and download a content in that directory. – Teapot Oct 06 '22 at 18:46
  • I have added the permission to read write external storage directory in manifest file. – Teapot Oct 06 '22 at 18:58
  • android 12 does not support write and read permission. see this https://stackoverflow.com/q/62782648/16765223 – M DEV Oct 07 '22 at 18:34
  • So, if i can create a public directory from android framework at boot time. Then i will be able to download a file in it. @MDEV am i right? – Teapot Oct 10 '22 at 07:45
  • If you just want to store images in the `Download` directory. You can save that Runtime in android (11-12) by using `contentResolver`. See this https://stackoverflow.com/a/56990305/16765223 – M DEV Oct 10 '22 at 07:59
  • See my question is how we can create a public directory like Downloads, DCIM etc. in android. And I need to change in AOSP framework to do so. But i don't know how the public directories are been created from. – Teapot Oct 11 '22 at 19:23
  • @MDEV If i can do as i mentioned in above comment, then I will be able to use download manager to download files. And this can not be down just from application layer. – Teapot Oct 11 '22 at 19:26
  • In android 11, app cannot create their own directory in public directory. See this https://stackoverflow.com/a/64579157/16765223 If you want to create directory in public folder then use `SAF` API. Ducument tree helps good. It ask user for permission, if permission granted, it create your app directory and you can easily anything access from it. See this https://stackoverflow.com/a/67554693/16765223 – M DEV Nov 19 '22 at 06:56

0 Answers0