未引用JNI版本----MediaMetadataRetriever获取视频缩略图(真机4.0测试通过)

package com.example.thumanaildemo5;

import java.io.File;
import java.util.HashMap;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.media.ThumbnailUtils;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.MediaStore.Images;
import android.widget.ImageView;

public class MainActivity extends Activity {
	ImageView thumbnails;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		File file = new File("/sdcard/mp4/china.mp4");
		thumbnails =(ImageView)findViewById(R.id.imageView1); 
        Bitmap bitmap = createVideoThumbnail(file.getAbsolutePath(),50,50);
        if(bitmap.toString().isEmpty())
        	System.out.println("bitmap is null");
        else{
        	thumbnails.setImageBitmap(bitmap);
        	
        	
        }
          
	}
	@SuppressLint("NewApi")
	private Bitmap createVideoThumbnail(String url, int width, int height) {
		  Bitmap bitmap = null;
		  MediaMetadataRetriever retriever = new MediaMetadataRetriever();
		  int kind = MediaStore.Video.Thumbnails.MINI_KIND;
		  try {
		   if (Build.VERSION.SDK_INT >= 14) {
		    retriever.setDataSource(url, new HashMap<String, String>());
		   } else {
		    retriever.setDataSource(url);
		   }
		   bitmap = retriever.getFrameAtTime();
		  } 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, width, height,
		     ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
		  }
		  return bitmap;
		 }
}


参考资料: Android 获取网络视频缩略图【完美兼容各版本】



你可能感兴趣的:(未引用JNI版本----MediaMetadataRetriever获取视频缩略图(真机4.0测试通过))