重命名由android 10中的应用程序创建的Mediastore的文件.正在使用Android API 30,但在API 29中显示错误 [英] Rename file of the Mediastore which is created by app in android 10. Working on Android API 30 but shows error in API 29

查看:477
本文介绍了重命名由android 10中的应用程序创建的Mediastore的文件.正在使用Android API 30,但在API 29中显示错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,此renameFile(..)函数在Android API 30中正常工作.但是,在Android API 29中却无效,并显示如下错误:

Here, this renameFile(..) func is working in Android API 30. But, it is not working in Android API 29 and shows the error like :

java.lang.IllegalArgumentException:不允许移动不属于定义明确的集合的content://media/external/file/116

更新说明:

---开始---

为了使用sdk-29,我们必须使用Uri作为extUri = MediaStore.Downloads.getContentUri(MediaStore.VOLUME_EXTERNAL),例如:

private static Uri extUri = MediaStore.Downloads.getContentUri(MediaStore.VOLUME_EXTERNAL);

代替下面的代码.并将 MediaStore.Files.FileColumns 更新为 MediaStore.Downloads

in place of below code. And also update MediaStore.Files.FileColumns to MediaStore.Downloads

---结束---

Uri extUri = MediaStore.Files.getContentUri(MediaStore.VOLUME_EXTERNAL);
String relativeLocation = Environment.DIRECTORY_DOWNLOADS + File.separator + "AppFolder";

函数renameFile(...)

boolean renameFile(Context context, String newName, String displayName) {

    try {
        Long id = getIdFromDisplayName(displayName);
        ContentResolver contentResolver = context.getContentResolver();
        Uri mUri = ContentUris.withAppendedId(extUri, id);
        ContentValues contentValues = new ContentValues();

        contentValues.put(MediaStore.Files.FileColumns.IS_PENDING, 1);
        contentResolver.update(mUri, contentValues, null, null);

        contentValues.clear();
        contentValues.put(MediaStore.Files.FileColumns.DISPLAY_NAME, newName);
        // contentValues.put(MediaStore.Files.FileColumns.MIME_TYPE, "files/pdf");
        // contentValues.put(MediaStore.Files.FileColumns.RELATIVE_PATH, relativeLocation);
        // contentValues.put(MediaStore.Files.FileColumns.TITLE, "SomeName");
        // contentValues.put(MediaStore.Files.FileColumns.DATE_ADDED, System.currentTimeMillis() / 1000);
        // contentValues.put(MediaStore.Files.FileColumns.DATE_TAKEN, System.currentTimeMillis());
        contentValues.put(MediaStore.Files.FileColumns.IS_PENDING, 0);
        contentResolver.update(mUri, contentValues, null, null);
        return true;
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return false;
}

函数getIdFromDisplayName(...)

@RequiresApi(api = Build.VERSION_CODES.Q)
Long getIdFromDisplayName(String displayName) {
    String[] projection;
    projection = new String[]{MediaStore.Files.FileColumns._ID};

    // TODO This will break if we have no matching item in the MediaStore.
    Cursor cursor = getContentResolver().query(extUri, projection,
            MediaStore.Files.FileColumns.DISPLAY_NAME + " LIKE ?", new String[]{displayName}, null);
    assert cursor != null;
    cursor.moveToFirst();

    if (cursor.getCount() > 0) {
        int columnIndex = cursor.getColumnIndex(projection[0]);
        long fileId = cursor.getLong(columnIndex);

        cursor.close();
        return fileId;
    }
    return null;
}

推荐答案

java.lang.IllegalArgumentException:不允许移动内容://media/external/file/116,这不是定义明确的集合的一部分

java.lang.IllegalArgumentException: Movement of content://media/external/file/116 which isn't part of well-defined collection not allowed

因此,如果您使用集合,则不允许用于Android Q;

So it is for Android Q not allowed if you use the collection;

Uri extUri = MediaStore.Files.getContentUri(MediaStore.VOLUME_EXTERNAL);

但是允许使用定义明确的集合",例如:

But is is allowed for a 'well-defined collection' like:

Uri extUri = MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL);
// Use  "Pictures/MyFolder" for RELATIVE_PATH

我留给您查找其他定义明确的集合.

I leave it to you to find other well-defined collections.

我不知道为什么这仅适用于Android Q.

Why this is only for Android Q i dont know.

您可以在以下Java文件中看到该消息:

You can see the message in the java file: https://android.googlesource.com/platform/packages/providers/MediaProvider/+/refs/heads/master/src/com/android/providers/media/MediaProvider.java

报价:

     // We only support movement under well-defined collections
        switch (match) {
            case AUDIO_MEDIA_ID:
            case VIDEO_MEDIA_ID:
            case IMAGES_MEDIA_ID:
            case DOWNLOADS_ID:
                break;
            default:
                throw new IllegalArgumentException("Movement of " + uri
                        + " which isn't part of well-defined collection not allowed");
        }

如果重命名失败,请使用SAF(如前所述). 如何在Android仅知道其媒体内容Uri

If the rename fails use SAF (as mentioned before). How to rename a file in Android knowing only its media content Uri

这篇关于重命名由android 10中的应用程序创建的Mediastore的文件.正在使用Android API 30,但在API 29中显示错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆