1.获取拍照图片的路径
从照相机Activity返回的时候,会带一个名为data的Bitmap对象。是照片的缩略图。
如果加上如下代码,
则不会返回名为data的对象。但是会按照指定的路径保存原始图片
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri
.fromFile(new File(Environment
.getExternalStorageDirectory(), "camera.jpg")));
2.获取拍照后的data对象
Bundle extras = data.getExtras();
Bitmap myBitmap=null;
if(null!=extras){
Log.i("bb","isNull:"+(null==extras));
if(myBitmap!=null){
myBitmap.recycle();
}
myBitmap = (Bitmap) extras.get("data");
}else{
Uri uri = data.getData();
if (uri != null) {
myBitmap.recycle();
myBitmap = BitmapFactory.decodeFile(uri.getPath());
}
}
if (myBitmap!=null) {
//ImageView view=new ImageView(mContext);
mImageView.setImageBitmap(myBitmap);
}
3.实例
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Hide the window title.
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
imageView = (ImageView) this.findViewById(R.id.preview);
Button button = (Button) this.findViewById(R.id.cameraButton);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri
.fromFile(new File(Environment
.getExternalStorageDirectory(), "camera.jpg")));
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
startActivityForResult(intent, 0);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0 && resultCode == Activity.RESULT_OK) {
this.imageView.setImageDrawable(Drawable.createFromPath(new File(
Environment.getExternalStorageDirectory(), "camera.jpg")
.getAbsolutePath()));
// for(String s:bundle.keySet()){
// Log.v("cam.demo", s);
// }
Log.v("cam.demo", "====>" + data);
}
}
}