简体   繁体   English

将下载的视频从 url 保存到存储,不适用于真实设备(不支持路径)

[英]Save downloaded video from url to storage not working on real devices (Path not supported)

I am trying to save a video which I downloaded from url to my internal storage.我正在尝试将我从 url 下载的视频保存到我的内部存储中。 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.奇怪的是,它在我的个人真实设备(三星 j6)和我的模拟器上运行得非常好,但在其他一些设备(三星 s10,尤其是华为)上不断崩溃,我不知道如何修复它和其他线程主题没有解决我手头的问题。

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始终在我的测试设备上工作: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到我的清单

我认为你可以使用 getFilesDir()、getExternalFilesDir() 和 getExternalFilesDirs()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM