0

I am trying to save a video which I downloaded from url to my internal storage. The weird thing is that its working on my personal real device (Samsung j6) and my simulators perfectly fine, but keeps on crashing on some other devices (Samsung s10 and especially huaweis) I have no idea how to fix it and other threads about this topic did not solve my issue at hand.

Down below my code of asking for permission during runtime + downloading the file

if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) ==
                PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA) ==
                PackageManager.PERMISSION_GRANTED) {


            if (post.url != null) {
                holder.sharedPhoto.buildDrawingCache();
                Bitmap bmp = holder.sharedPhoto.getDrawingCache();


                if (ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA) ==
                        PackageManager.PERMISSION_GRANTED) {
                    MediaStore.Images.Media.insertImage(context.getContentResolver(), bmp, "Flax"+post.id, post.url);
                    new SweetAlertDialog(context, SweetAlertDialog.SUCCESS_TYPE)
                            .setTitleText(context.getString(R.string.SUCCESS_IMG_SAVED))
                            .show();
                }
            }

            if (post.vUrl != null && !post.vUrl.equals("")) {
                String mBaseFolderPath = android.os.Environment
                        .getExternalStorageDirectory().getPath()
                        + File.separator
                        + "FolderName" + File.separator;
                if (!new File(mBaseFolderPath).exists()) {
                    new File(mBaseFolderPath).mkdir();
                }

                String fname = post.date.toString();

                Uri downloadUri = Uri.parse(post.vUrl.trim());
                if (downloadUri == null) {
                    return;
                }

                String mFilePath = "file://" + mBaseFolderPath + "/" + fname ;
                DownloadManager.Request req = new DownloadManager.Request(downloadUri);
                req.setDestinationUri(Uri.parse(mFilePath));
                req.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                DownloadManager dm = (DownloadManager) context.getSystemService(context.DOWNLOAD_SERVICE);
                dm.enqueue(req);
                Toast.makeText(context, R.string.video_downloaded, Toast.LENGTH_LONG).show();

            }
        }else {
            ActivityCompat.requestPermissions((Activity) context, new String[]
                    {Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
        }

Works always on my test devices :D

my error code is the following

Fatal Exception: java.lang.SecurityException
Unsupported path /storage/emulated/0/FolderName/1604782936

More of the log

Fatal Exception: java.lang.SecurityException: Unsupported path /storage/emulated/0/FolderName/1604782936
   at android.os.Parcel.createException(Parcel.java:2071)
   at android.os.Parcel.readException(Parcel.java:2039)
   at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:188)
   at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:140)
   at android.content.ContentProviderProxy.insert(ContentProviderNative.java:481)
   at android.content.ContentResolver.insert(ContentResolver.java:1844)
   at android.app.DownloadManager.enqueue(DownloadManager.java:1081)
   at com.flax.de.Cells.PostTabelViewCell.savePost(PostTabelViewCell.java:821)
   at com.flax.de.Cells.PostTabelViewCell.access$1900(PostTabelViewCell.java:78)
   at com.flax.de.Cells.PostTabelViewCell$23.onClick(PostTabelViewCell.java:643)
   at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:1229)
   at android.widget.AdapterView.performItemClick(AdapterView.java:330)
   at android.widget.AbsListView.performItemClick(AbsListView.java:1259)
   at android.widget.AbsListView$PerformClick.run(AbsListView.java:3306)
   at android.widget.AbsListView$3.run(AbsListView.java:4296)
   at android.os.Handler.handleCallback(Handler.java:888)
   at android.os.Handler.dispatchMessage(Handler.java:100)
   at android.os.Looper.loop(Looper.java:213)
   at android.app.ActivityThread.main(ActivityThread.java:8178)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)

Any idea how to fix it to work on all devices?

i have added the

        android:requestLegacyExternalStorage="true"

to my manifest

Zash__
  • 293
  • 4
  • 16
  • What you want will not be supported on Android 11 once your `targetSdkVersion` reaches 30. So, you may wish to start considering other alternatives, such as not using `DownloadManager` and downloading the content to directories that you will be allowed to write to. – CommonsWare Nov 08 '20 at 13:26
  • thank you for the hint. Could you maybe share a link to where somebody achieves what I want and which would work on all devices? – Zash__ Nov 08 '20 at 18:23
  • I do not know what you want, beyond what you have in your question. And what you want -- to write to arbitrary directories off of the root of external storage using `DownloadManager` -- is not supported on Android 10+. At best, if you [download directly](https://stackoverflow.com/a/29012988/115145), you will buy yourself a few months. The #1 thing is to stop thinking in terms of custom directories off of the root of external storage. [This blog post series](https://commonsware.com/blog/2019/10/19/scoped-storage-stories-saf-basics.html) covers some options. – CommonsWare Nov 08 '20 at 18:28

1 Answers1

0

I think you can use getFilesDir(), getExternalFilesDir() and getExternalFilesDirs()