package com.example.videocapture;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.MediaStore.Video.Thumbnails;
import android.provider.MediaStore.Video.VideoColumns;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private static final int VIDEO_CAPTURE = 0;
private ImageView imageView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView = (ImageView) findViewById(R.id.imageView);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(intent, VIDEO_CAPTURE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK && requestCode == VIDEO_CAPTURE) {
Uri uri = data.getData();
Cursor cursor = this.getContentResolver().query(uri, null, null,
null, null);
if (cursor != null && cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndex(VideoColumns._ID));
String filePath = cursor.getString(cursor.getColumnIndex(VideoColumns.DATA));
Bitmap bitmap = Thumbnails.getThumbnail(getContentResolver(),
id, Thumbnails.MICRO_KIND, null);
//ThumbnailUtils类2.2以上可用
// Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(filePath, Thumbnails.MICRO_KIND);
imageView.setImageBitmap(bitmap);
cursor.close();
}
}
}
}
参考:http://my.oschina.net/chengliqun/blog/62150
http://aichixihongshi.iteye.com/blog/1233825
http://www.cnblogs.com/jjyoung/archive/2012/04/13/2446073.html