1

我有一个活动,它调用相机意图来捕获图片并将其分配为个人资料图片。该活动工作正常,除了当我单击返回然后再次打开该活动时,图片不再显示。

每次用户打开此活动时,如何使其显示?这是我的那个活动的代码

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(),但我的应用程序崩溃了。任何帮助深表感谢。

4

1 回答 1

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)
于 2012-10-20T05:37:08.277 回答