如何重命名从 android 媒体存储中获取的视频?
How can I rename a video gotten from android mediastore?
如何重命名从 Mediastore 获取的视频而不删除或破坏它?
我尝试使用从类似问题中获得的这段代码,但它删除或破坏了我更改名称的视频。
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(); });
如何重命名从 Mediastore 获取的视频而不删除或破坏它?
我尝试使用从类似问题中获得的这段代码,但它删除或破坏了我更改名称的视频。
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(); });