Creating a thumbnail from a video file returns a zero bitmap

I send the intention to start the camcorder

PackageManager pm = getPackageManager(); if(pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)){ Intent video = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); File tempDir= new File(Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES), "BCA"); if(!tempDir.exists()) { if(!tempDir.mkdir()){ Toast.makeText(this, "Please check SD card! Image shot is impossible!", Toast.LENGTH_SHORT).show(); } } String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",Locale.US).format(new Date()); File mediaFile = new File(tempDir.getPath() + File.separator + "VIDEO_"+ timeStamp + ".mp4"); Uri videoUri = Uri.fromFile(mediaFile); video.putExtra(MediaStore.EXTRA_OUTPUT, videoUri); video.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); startActivityForResult(video, VIDEO_REQUEST); }else{ Toast.makeText(this, "This device does not have a rear facing camera",Toast.LENGTH_SHORT).show(); } 

I take the video and it is stored correctly. When onActivityResult launched, I use the intent to get the uri where it is stored to create a bitmap

this is an example uri file:///storage/emulated/0/Pictures/BCA/VIDEO_20131227_145043.mp4

  Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(intent.getDataString(), MediaStore.Video.Thumbnails.MICRO_KIND); 

but the bitmap is null every time. Therefore, since the docs say May return null if the video is corrupt or the format is not supported , I check the video in the directory and plays its .mp4 file perfectly, which is supported, so what am I doing wrong here?

+7
source share
4 answers

You can try MediaMetadataRetriever or FFmpegMediaMetadataRetriever . Here is an example:

 FFmpegMediaMetadataRetriever mmr = new FFmpegMediaMetadataRetriever(); mmr.setDataSource(intent.getDataString()); Bitmap b = mmr.getFrameAtTime(); mmr.release(); 
+3
source

As I recall, the filePath argument from createVideoThumbnail should be a classic file path, not a URI .

 ... Uri videoUri = intent.getData(); final String realFilePath = getRealPathFromUri(); Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(realFilePath, MediaStore.Video.Thumbnails.MICRO_KIND); ... public String getRealPathFromURI(final Uri contentURI) { Cursor cursor = getContentResolver().query(contentURI, null, null, null, null); if (cursor == null) { // Source is Dropbox or other similar local file path return contentURI.getPath(); } else { cursor.moveToFirst(); int idx = cursor.getColumnIndex(MediaStore.MediaColumns.DATA); if ( idx == -1 ) { return contentURI.getPath(); } String rvalue = cursor.getString(idx); cursor.close(); return rvalue; } } 

EDIT:

Based on the createVideoThumbnail source code, I created another implementation:

 public static Bitmap createVideoThumbnail(Context context, Uri uri, int kind) { Bitmap bitmap = null; MediaMetadataRetriever retriever = new MediaMetadataRetriever(); try { retriever.setMode(MediaMetadataRetriever.MODE_CAPTURE_FRAME_ONLY); retriever.setDataSource(context, uri); bitmap = retriever.captureFrame(); } catch (IllegalArgumentException ex) { // Assume this is a corrupt video file } catch (RuntimeException ex) { // Assume this is a corrupt video file. } finally { try { retriever.release(); } catch (RuntimeException ex) { // Ignore failures while cleaning up. } } if (kind == Images.Thumbnails.MICRO_KIND && bitmap != null) { bitmap = ThumbnailUtils.extractThumbnail(bitmap, ThumbnailUtils.TARGET_SIZE_MICRO_THUMBNAIL, ThumbnailUtils.TARGET_SIZE_MICRO_THUMBNAIL, ThumbnailUtils.OPTIONS_RECYCLE_INPUT); } return bitmap; } 
+2
source

Use this mediaFile and convert it to a URI

  Uri uri=Uri.fromFile(mediaFile); 

Then pass this URI in the following method. This works great on my side.

 Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(uri.getPath(), MediaStore.Video.Thumbnails.MICRO_KIND); 

Hope this helps you.

+1
source

I ran into this problem and solved it like this:

  • Create FileUtils A class that will find the path to the file for you (I cannot find a reference to the class, so I created the Entity)

     String correctedUri = FileUtils.getPath(mContext, Uri.parse(localUri)); 
  • Use the following code

     Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(correctedUri, MediaStore.Video.Thumbnails.MICRO_KIND); 

EDITED: See this solution, which has better performance and easier.

0
source

All Articles