在这里,这个 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)