java—重命名android 10中应用程序创建的mediastore文件正在使用android api 30,但在api 29中显示错误

hof1towb  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(1043)

这里,这个renamefile(..)函数在android api 30中工作。但是,它在android api 29中不起作用,并显示如下错误:
java.lang.illegalargumentexception:移动content://media/external/file/116 这不是定义良好的集合的一部分
更新说明:
---开始---
为了使用sdk-29,我们必须使用exturi=mediastore.downloads.getcontenturi(mediastore.volume\u external)这样的uri:

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

代替下面的代码。并将mediastore.files.filecolumns更新为mediastore.downloads
---结束---

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

函数重命名文件(…)

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;
}
b4lqfgs4

b4lqfgs41#

java.lang.illegalargumentexception:移动content://media/external/file/116 这不是定义良好的集合的一部分
所以对于android q来说,如果你使用这个集合是不允许的;

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

但允许“定义明确的集合”,如:

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

我把它留给你去寻找其他定义良好的集合。
为什么这只适用于android q我不知道。
您可以在java文件中看到以下消息: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的文件

相关问题