Generate Thumbnail From Sdcard in Android Q

Viewed 1336

I am using below method to generate thumbnail (its deprecated in android 10)

thumbnail_bitmap = ThumbnailUtils.createVideoThumbnail(path, MediaStore.Video.Thumbnails.FULL_SCREEN_KIND);

Getting Exception like this

java.io.IOException: Failed to create thumbnail

Thank you.

2 Answers

This is solution for generate thumbnail of video from storage.

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {

            thumbnail_bitmap = createThumbnail(RecordPitchActivity.this, path);

        } else {
            thumbnail_bitmap = ThumbnailUtils.createVideoThumbnail(path, MediaStore.Video.Thumbnails.FULL_SCREEN_KIND);
        }
public static Bitmap createThumbnail(Activity activity, String path) {
        MediaMetadataRetriever mediaMetadataRetriever = null;
        Bitmap bitmap = null;
        try {
            mediaMetadataRetriever = new MediaMetadataRetriever();
            mediaMetadataRetriever.setDataSource(activity, Uri.parse(path));
            bitmap = mediaMetadataRetriever.getFrameAtTime(1000, MediaMetadataRetriever.OPTION_CLOSEST_SYNC);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (mediaMetadataRetriever != null) {
                mediaMetadataRetriever.release();
            }
        }
        return bitmap;
    }

There is a few things wrong with your implementation.

The first is createVideoThumbnail was changed from:

public static Bitmap createVideoThumbnail (String filePath, int kind)

to

public static Bitmap createVideoThumbnail (File file, Size size, CancellationSignal signal)


The second is the way you retrieve your path and trying to convert it to a Uri:

String path = getExternalFilesDir(Environment.DIRECTORY_MOVIES) .getAbsolutePath()+ "/" + new SimpleDateFormat("yyyyMM_dd-HHmmss").format(new Date()) + "_compress.mp4";

Uri fileUri = FileProvider.getUriForFile(this, getPackageName() + ".provider", new File(path));

You should instead make use of ContentValues and ContentResolver to create a Uri, then write to that Uri and insert it when done, like this:

ContentValues valuesvideos = new ContentValues();
valuesvideos.put(MediaStore.Images.Media.RELATIVE_PATH, "Movies/");
valuesvideos.put(MediaStore.Images.Media.TITLE, fileName);
valuesvideos.put(MediaStore.Images.Media.DISPLAY_NAME, fileName);
valuesvideos.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
valuesvideos.put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis() / 1000);
valuesvideos.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
valuesvideos.put(MediaStore.Images.Media.IS_PENDING, 1); 
ContentResolver resolver = getContentResolver(); 

Uri collection = MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY); 

Size mSize = new Size(96,96);
CancellationSignal ca = new CancellationSignal(); 
Bitmap bitmapThumbnail = getContentResolver().loadThumbnail(collection, mSize, ca);

Uri uriSavedThumb = resolver.insert(collection, valuesvideos);
Related