本文介绍了java.lang.IllegalArgumentException:所有请求的项目必须由特定 ID 引用(Android)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在调用 MediaStore.createWriteRequest(contentResolver, uris) 时收到此异常.与在 Anrdroid Q 及更高版本中一样,我们必须让 createWriteRequest 写入存储.所以我正在尝试以下代码并获得异常.

I'm getting this exception upon calling MediaStore.createWriteRequest(contentResolver, uris). As in Anrdroid Q and above we have to make createWriteRequest to write on storage. So I'm trying the following code and getting the exception.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            List<Uri> uris = new ArrayList<>();
            uris.add(mediaUri);
            MediaStore.createWriteRequest(contentResolver, uris);
            //code
        }

推荐答案

您的 Uri 路径错误,这就是显示错误消息的原因.

无效的 Uri 路径: content://com.abc.photoapp.provider/external_files/emulated/0/Pictures/camara/1623819097267.jpg

Invalid Uri Path: content://com.abc.photoapp.provider/external_files/emulated/0/Pictures/camara/1623819097267.jpg

有效路径为:content://media/external/images/media/52703

Valid Path is :content://media/external/images/media/52703

在这里,我一步一步解释删除媒体的文件路径.

第 1 步:

假设你有一个这样的文件路径/storage/emulated/0/tempPic/export_image.jpg"

Suppose You have a file path like this"/storage/emulated/0/tempPic/export_image.jpg"

File tempFile=new File("/storage/emulated/0/tempPic/export_image.jpg");
long mediaID=getFilePathToMediaID(tempFile.getAbsolutePath(),  context);

public long getFilePathToMediaID(String songPath, Context context)
{
        long id = 0;
        ContentResolver cr = context.getContentResolver();

        Uri uri = MediaStore.Files.getContentUri("external");
        String selection = MediaStore.Audio.Media.DATA;
        String[] selectionArgs = {songPath};
        String[] projection = {MediaStore.Audio.Media._ID};
        String sortOrder = MediaStore.Audio.Media.TITLE + " ASC";

        Cursor cursor = cr.query(uri, projection, selection + "=?", selectionArgs, null);

        if (cursor != null) {
            while (cursor.moveToNext()) {
                int idIndex = cursor.getColumnIndex(MediaStore.Audio.Media._ID);
                id = Long.parseLong(cursor.getString(idIndex));
            }
        }

        return id;
}

第 2 步:

为 Uri 创建媒体 ID

Create media id to Uri

Uri Uri_one = ContentUris.withAppendedId( MediaStore.Images.Media.getContentUri("external"),mediaID);

// content://media/external/images/media/52703

内容 Uri 根据您的文件类型而变化:

The content Uri change according to your file type:

如果图像则:MediaStore.Images.Media.getContentUri(external")如果是视频则:MediaStore.Video.Media.getContentUri(external")

If Image then : MediaStore.Images.Media.getContentUri("external")If Video then : MediaStore.Video.Media.getContentUri("external")

第 3 步:

准备并调用删除

List<Uri> uris=new ArrayList<>();
uris.add(<Add Paht : Uri_one >);
uris.add(<Add Paht : Uri_two >);
uris.add(<Add Paht : Uri_three >);

requestDeletePermission( context,uris);

在此处输入图片描述调用该方法后,询问权限对话框.requestDeletePermission 方法是 onActivityResult 方法的返回结果.

enter image description hereAfter you call the method ask permission dialog. requestDeletePermission method is return result on onActivityResult method.

上述方法支持 android 11(目标 SDK 版本 30)及更高版本.删除媒体文件不需要 manage_external_storage 权限.您可以使用视频、音频和图像等媒体.

The above method support android 11 (Target SDK Version 30 ) and a greater version. You do not require manage_external_storage permission for media file delete. You can use media like Video, Audio, and Images.

如果您要删除文档文件,则必须要求 manage_external_storage 权限.

If you want to delete a document file so you must require manage_external_storage permission.

这篇关于java.lang.IllegalArgumentException:所有请求的项目必须由特定 ID 引用(Android)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 16:25