相关疑难解决方法(0)

重命名由应用程序在 android 10 中创建的 Mediastore 文件。在 Android API 30 上工作,但在 API 29 中显示错误

在这里,这个 renameFile(..) func 在 Android API 30 中工作。但是,它在 Android API 29 中不起作用并显示如下错误:

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

更新-注意:

---开始---

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

private static Uri extUri = MediaStore.Downloads.getContentUri(MediaStore.VOLUME_EXTERNAL);
Run Code Online (Sandbox Code Playgroud)

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

---结束---

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

函数重命名文件(...)

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, …
Run Code Online (Sandbox Code Playgroud)

java android file-rename mediastore android-10.0

2
推荐指数
1
解决办法
2537
查看次数

标签 统计

android ×1

android-10.0 ×1

file-rename ×1

java ×1

mediastore ×1