Attempt to invoke virtual method ‘android.os.Bundle android.content.Intent.getExtras()‘解决方法

在写安卓的时候,想实现拍照并显示的功能。方法就是用intent调用相机,然后在onActivityResult方法里把结果取出来并显示。

Bundle bundle = data.getExtras(); 
Bitmap bitmap = (Bitmap) bundle.get("data"); 
imageview.setImageBitmap(bitmap);

结果报错:

Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference

这时候需要检查,打开相机的时候是否有

captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);

如果使用了Intent的putExtra方法,onActivityResult是取不到数据的,因为putExtra是把缓存写到存储,写完之后缓存清空,onActivityResultdata是从缓存读取,当然就读不到了。

解决方法

1.可以选择把putExtra注释掉,不存图片,只读一次。
2.也可以选择存储照片,再把照片从外存上读进来。

 bitmap = BitmapFactory.decodeStream(this.getContentResolver().openInputStream(photoUri));
 imageview.setImageBitmap(bitmap);

你可能感兴趣的:(android,android)