我有一个活动,调用相机意图捕获图片并将其指定为个人资料图片。活动工作正常,但是当我再次点击然后再次打开活动时,图片不再显示。
每次用户打开此活动时,如何让它显示?这是我的活动代码
public class MyAccountActivity extends Activity {
private static final int CAMERA_REQUEST = 1888;
private TextView name;
private TextView userId;
private TextView address;
private TextView email;
private TextView phone;
private ImageButton profilePicture;
private Bitmap bm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_account);
setUpViews();
Log.v("test","this is test: "+LoginActivity.user.getName());
}
private void setUpViews() {
//setting up views
//calling user details from User [] instance
}
public void ViewPicture(View v) {
Intent intent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI.getPath());
startActivityForResult(intent, CAMERA_REQUEST);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
bm = (Bitmap) data.getExtras().get("data");
profilePicture.setImageBitmap(bm);
MediaStore.Images.Media.insertImage(getContentResolver(), bm, null, null);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
byte[] b = baos.toByteArray();
}
我尝试拨打profilePicture.setImageBitmap(bm)
onResume()
,但我的应用崩溃了。
非常感谢任何帮助。
答案 0 :(得分:0)
使用一些uri路径保存捕获的图像
Uri uri = Uri.withAppendedPath(Uri.fromFile(context.getExternalFilesDir(null)), imageName.png);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, uri));
然后,只要您想显示图像,请使用此uri
profilePicture.setImageUri(uri)