DocumentFile.renameTo uses old filename when sharing file through gallery

264 views Asked by At

I have an app that renames images and use the Document Fileprovider to be able to rename files also on the external storage. The renaming works fine, when I check the filename via a filemanager and also when I view the the "Info" in the Google Photos app the new filename shows. But when I share the file via Google Photos and choose "Mail" for example, the attachment picks up the old filename.

I tried updating the media store with:

Bundle bundle = new Bundle();
bundle.putString("volume", "external");
context.startService(new Intent().setComponent(new ComponentName("com.android.providers.media", "com.android.providers.media.MediaScannerService")).putExtras(bundle));

and

Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(newFile));
context.sendBroadcast(intent);

or

private static void updateMediaStore(Context context, File oldFile, File newFile, long taken)
{
    deleteImageFromMediaStore(context, oldFile.getAbsolutePath());
    addImageToMediaStore(context.getContentResolver(), newFile, taken);

}

private static void deleteImageFromMediaStore(Context context, String path)
{
    Uri rootUri = MediaStore.Audio.Media.getContentUriForPath( path );

    context.getContentResolver().delete( rootUri,
            MediaStore.MediaColumns.DATA + "=?", new String[]{ path } );
}

private static void addImageToMediaStore(ContentResolver cr, File filepath, long taken) {
    ContentValues values = new ContentValues();
    values.put(MediaStore.Images.Media.TITLE, filepath.getName());
    values.put(MediaStore.Images.Media.DISPLAY_NAME, filepath.getName());
    values.put(MediaStore.Images.Media.DESCRIPTION, "");
    values.put(MediaStore.Images.Media.MIME_TYPE, Helper.isImageFile(filepath.getAbsolutePath()) ? "image/jpeg": "video/mp4");
    values.put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis());
    values.put(MediaStore.Images.Media.DATE_TAKEN, taken);
    values.put(MediaStore.Images.Media.DATA, filepath.toString());

    cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
}

...also tried some 3rd party media scanners from the playstore. Rebooted too, since that triggers a media scan as well.

But the problem still exists.

Added some images that shows the issue I describe

Correct Filename in Files app

Correct Filename in Info dialog of Google Photos app

Incorrect old Filaname after sharing photo via Google Photo as mail attachment

0

There are 0 answers