Refreshing the thumbnail using MediaScanner

坚强是说给别人听的谎言 提交于 2019-12-04 20:35:58

This is a common and well know problem in Android. If you edit a media file, the thumbnail does not seem to update.

I have a fix for this, however, its still a fix and not a clean solution. My fix is simple, and it basically deletes the stale thumbnail and then uses media scanner to update the thumbnails.

Here're the steps to be followed:

Step 1. Edit the file as you like. Say filename, "myVideoToBeEdited".

Step 2. Once you are done editing, delete its existing thumbnail. First, get the video id using code like this:

                final String[] columns = {
                    BaseColumns._ID, MediaColumns.DATA
                };

            ContentResolver cr = context.getContentResolver();
            Cursor cursor = cr.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, columns, null, null, null);

            boolean cancel = false;
            if(null != cursor){
                while(cursor.moveToNext() && !cancel){
                    String fileName = cursor.getString(cursor.getColumnIndex(MediaColumns.DATA));
                    int imageId = cursor.getInt(cursor.getColumnIndex(BaseColumns._ID));

                    if(fileName.equals(myVideoToBeEdited)){
                        removeVideoThumbnail(getContentResolver(), imageId); // step 3
                        cancel = true;
                    }
                }
            }

There are other ways to get the id, and more optimised ones as well.

Step 3. Delete the thumbnail.

        public void removeVideoThumbnail(ContentResolver contentResolver, long photoId) {
        Cursor thumbnails = contentResolver.query(android.provider.MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI, null, android.provider.MediaStore.Video.Thumbnails.VIDEO_ID + "=?", new String[]{String.valueOf(photoId)}, null);
        for (thumbnails.moveToFirst(); !thumbnails.isAfterLast(); thumbnails.moveToNext()) {

            long thumbnailId = thumbnails.getLong(thumbnails.getColumnIndex(android.provider.MediaStore.Video.Thumbnails._ID));
            String path = thumbnails.getString(thumbnails.getColumnIndex(android.provider.MediaStore.Video.Thumbnails.DATA));
            File file = new File(path);
            if (file.delete()) {

                contentResolver.delete(android.provider.MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI, android.provider.MediaStore.Video.Thumbnails._ID + "=?", new String[]{String.valueOf(thumbnailId)});

            }

        }
    }

Or, here's the method to delete image thumbnail

    public void removeImageThumbnail(ContentResolver contentResolver, long photoId) {
        Cursor thumbnails = contentResolver.query(android.provider.MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, null, android.provider.MediaStore.Images.Thumbnails.IMAGE_ID + "=?", new String[]{String.valueOf(photoId)}, null);
        for (thumbnails.moveToFirst(); !thumbnails.isAfterLast(); thumbnails.moveToNext()) {

            long thumbnailId = thumbnails.getLong(thumbnails.getColumnIndex(android.provider.MediaStore.Images.Thumbnails._ID));
            String path = thumbnails.getString(thumbnails.getColumnIndex(android.provider.MediaStore.Images.Thumbnails.DATA));
            File file = new File(path);
            if (file.delete()) {

                contentResolver.delete(android.provider.MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, android.provider.MediaStore.Images.Thumbnails._ID + "=?", new String[]{String.valueOf(thumbnailId)});

            }

        }
    }

Step 4. And finally use media scanner connection to scan the file so that it updates the thumbnails.

         MediaScannerConnection.scanFile(context,
              new String[] { myVideoToBeEdited }, null,
              new MediaScannerConnection.OnScanCompletedListener() {
          public void onScanCompleted(String path, Uri uri) {
                      // pass the mime type, else passing a null will enable file extension to dictate the mime type
              // you are good to go
          }
     });

Have you tried to remove the "old" picture prior to saving the new one to file system? Like so:

File file = new File(SDCARD_PATH, filename);

try {
    // Delete the "old" file.
    if (file.exists()) {
        file.delete();
    }

    FileOutputStream out = new FileOutputStream(file);
    bmp.compress(format, BEST_IMAGE_QUALITY, out);
}catch (FileNotFoundException e) {
}catch (SecurityException e) {
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!