How can I rename a video gotten from mediastore without deleting or corrupting it?
I tried using this code gotten from a similar question but it deletes or corrupts the video whose name I changed.
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();
}
}
Finally solved it
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();
});