在测试相机意图类时,我尝试根据用户点击的照片设置 imageView 在低于 Android 3.0 的设备上失败了(imageView 不显示拍摄的图像)。该类在后者的 Android 设备中运行良好。
此代码是否需要进行任何更改,以便低于 Android 3.0 的设备也可以支持此项目?
初始定义
private static final int TAKE_PICTURE = 0;
private Uri mUri;
private Bitmap mPhoto;
button.onClick 方法:
Intent i = new Intent("android.media.action.IMAGE_CAPTURE");
File f = new File(Environment.getExternalStorageDirectory(), "photo.jpg");
i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
mUri = Uri.fromFile(f);
startActivityForResult(i, TAKE_PICTURE);
最后是onActivityResult:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case TAKE_PICTURE:
if (resultCode == Activity.RESULT_OK) {
getContentResolver().notifyChange(mUri, null);
ContentResolver cr = getContentResolver();
try {
mPhoto = android.provider.MediaStore.Images.Media.getBitmap(cr, mUri);
((ImageView)findViewById(R.id.photo_holder)).setImageBitmap(mPhoto);
} catch (Exception e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
}
}