如何重命名从媒体存储获取的视频而不删除或损坏它?
我尝试使用从类似问题获得的代码,但它删除或损坏了我更改名称的视频。
String title = editTextTitle.getText().toString().trim();
video.setTitle(title);
File parentFile = videoFile.getParentFile();
if (parentFile != null) {
String path = parentFile.getAbsolutePath();
String extension = videoFile.getAbsolutePath(); extension = extension.substring(extension.lastIndexOf("."));
String newPath = path + "/" + title + extension;
File newFile = new File(newPath);
boolean rename = videoFile.renameTo(newFile); if (rename) {
ContentResolver resolver = context.getContentResolver(); resolver.delete(MediaStore.Files.getContentUri("external"), MediaStore.MediaColumns.DATA + "=?", new String[]{videoFile.getAbsolutePath()});
Toast.makeText(context, "renamed", Toast.LENGTH_SHORT). show();
}
}
终于解决了
textViewRename.setOnClickListener(v1 -> {
String title = editTextTitle.getText().toString().trim();
video.setTitle(title);
String extension = videoFile.getAbsolutePath();
extension = extension.substring(extension.lastIndexOf("."));
ContentValues values = new ContentValues(2);
values.put(MediaStore.Video.Media.TITLE, title);
values.put(MediaStore.Video.Media.DISPLAY_NAME, title + extension);
context.getContentResolver().update(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values,
MediaStore.MediaColumns.DATA + "=?", new String[]{videoFile.getAbsolutePath()});
if (videoOptionListener != null) {
videoOptionListener.onEdit();
}
dialogRenameVideo.dismiss();
});