Android Java download Video and save to internal storage

Viewed 217

I am achieving this by the following code

                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();

unfortunately this only works on my simulator but on NO real device. I am asking for permission and downloading images works on ALL devices, but video not.

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

is the error 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)

even though I am asking for permission and downloading images works fine.

My app is like instagram and I want users to be able to download the video straight to their phone, like on TikTok

i have added

android:requestLegacyExternalStorage="true"

to my manifest file

0 Answers
Related