编辑:我选择的外部文件夹是文档文件夹,供参考。
最终我终于让它工作了。这里是重命名视频的代码(可能不是最好的,但它能完成任务!)
private static void tryAddVideosToMediaStore(Activity context) {
List<File> files = MediaUtils.getVideoFilesFromDirectory();
for (File file : files) {
try {
Uri fromUri = FileProvider.getUriForFile(context, context.getPackageName() + ".provider", file);
if (getRealPathFromURI(context, fromUri) == null) {
String nameWoExtension = MediaUtils.getNameWithoutStatus(file.getAbsolutePath());
ContentValues values = new ContentValues(3);
values.put(MediaStore.Video.Media.TITLE, nameWoExtension);
values.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
values.put(MediaStore.Video.Media.DATA, file.getAbsolutePath());
context.getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static String getRealPathFromURI(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = {MediaStore.Images.Media.DATA};
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} catch(Exception e) {
return null;
}finally {
if (cursor != null) {
cursor.close();
}
}
}
然后是调用方法
public static String getVideoNameFromPath(String path) {
return path.substring(path.lastIndexOf("/") + 1, path.indexOf(".mp4"));
}
public static boolean renameVideoFile(MainActivityViewModel viewModel, SharedPreferenceHelper sharedPreferenceHelper, Activity c, File from, File to) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
tryAddVideosToMediaStore(c);
Uri fromUri = MediaUtils.getVideoUriFromFS(c, from);
try {
ContentResolver contentResolver = c.getContentResolver();
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.Files.FileColumns.IS_PENDING, 1);
contentResolver.update(fromUri, contentValues, null, null);
contentValues.clear();
contentValues.put(MediaStore.Files.FileColumns.DISPLAY_NAME, to.getName());
contentValues.put(MediaStore.Files.FileColumns.IS_PENDING, 0);
contentResolver.update(fromUri, contentValues, null, null);
return true;
} catch (Exception securityException) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
sharedPreferenceHelper.get().edit().putString("from", from.getAbsolutePath()).putString("to", to.getAbsolutePath()).apply();
RecoverableSecurityException recoverableSecurityException;
viewModel.setContentUri(fromUri);
if (securityException instanceof RecoverableSecurityException) {
recoverableSecurityException =
(RecoverableSecurityException) securityException;
} else {
requestVideoWritePermissions(c, Uri.parse(MediaStore.Video.Media.EXTERNAL_CONTENT_URI + "/" + MediaUtils.getVideoId(c, from)));
return false;
}
IntentSender intentSender = recoverableSecurityException.getUserAction()
.getActionIntent().getIntentSender();
try {
c.startIntentSenderForResult(intentSender, 55,
null, 0, 0, 0);
} catch (Exception e) {
e.printStackTrace();
return false;
}
} else {
throw new RuntimeException(
securityException.getMessage(), securityException);
}
}
return false;
} else {
if (from.renameTo(to)) {
removeMedia(c, from);
addMedia(c, to);
return true;
} else {
return false;
}
}
}
public static Uri getVideoUriFromFS(Context c, File file) {
long id = getFilePathToMediaID(file, c);
Uri fromUri = ContentUris.withAppendedId( MediaStore.Video.Media.EXTERNAL_CONTENT_URI,id);
return fromUri;
}
public static long getFilePathToMediaID(File videoPath, Context context)
{
Uri mainUri;
Cursor cursor1 = context.getContentResolver().query(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
new String[]{MediaStore.Video.Media._ID},
MediaStore.Video.Media.DATA + "=? ",
new String[]{videoPath.getAbsolutePath()}, null);
long id = 0;
if (cursor1 != null && cursor1.moveToFirst()) {
id = cursor1.getLong(cursor1.getColumnIndex(MediaStore.MediaColumns._ID));
cursor1.close();
}
return id;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 55) {
if (resultCode == RESULT_OK) {
String from = presenter.getFromFilePath();
String to = presenter.getToFilePath();
if (from != null && to != null) {
Uri fromUri = MediaUtils.getVideoUriFromFS(this, new File(from));
ContentResolver contentResolver = getContentResolver();
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.Files.FileColumns.IS_PENDING, 1);
contentResolver.update(fromUri, contentValues, null, null);
contentValues.clear();
contentValues.put(MediaStore.Files.FileColumns.DISPLAY_NAME, new File(to).getName());
contentValues.put(MediaStore.Files.FileColumns.IS_PENDING, 0);
contentResolver.update(fromUri, contentValues, null, null);
}
}
}
}
如果我忘了什么,请告诉我,我会在这里发布。 搜寻了几个小时才找到这个解决方案,对于谷歌引入的简单性与复杂性之间的差异感到相当沮丧。
编辑:我想我忘记了这个非常重要的方法。
public static boolean requestVideoWritePermissions(Activity activity, Uri fromUri) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
boolean hasPermission = true;
if (activity.checkUriPermission(fromUri, Binder.getCallingPid(), Binder.getCallingUid(),
Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != PackageManager.PERMISSION_GRANTED) {
hasPermission = false;
}
List<Uri> uriList = new ArrayList<>();
uriList.add(fromUri);
if (!hasPermission) {
PendingIntent pi = MediaStore.createWriteRequest(activity.getContentResolver(), uriList);
try {
activity.startIntentSenderForResult(pi.getIntentSender(), 55, null, 0, 0, 0);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
return false;
}
return true;
}
return true;
}
我还应该提到每个视频都是这种方式的提示。用户可以选择是否允许您覆盖每个视频,这不太理想。我希望我可以通过外部访问来处理整个文件夹,但我猜测这在作用域存储更改中不会发生。