在测试相机意图类时,我尝试根据用户点击的照片设置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();
}
}
}
}
答案 0 :(得分:1)
您应该将uri保存在onSaveInstanceState中。由于旧设备内存不足,当他们启动新应用程序(如相机)时会停止应用程序。摄像机的活动完成后,它会再次从保存的状态重新创建您的活动。
@Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
if (mUri!= null) {
outState.putString("cameraImageUri", mUri.toString());
}
}
并在你的onCreate做类似的事情
if (savedInstanceState != null)
{
mUri=Uri.parse(savedInstanceState.getString("cameraImageUri"));
}