如何解决当我尝试在调用相机活动时使用Intent.putExtra()
时发生的空指针异常。
public class ImageCaptureActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Uri mImageCaptureUri = Uri.fromFile(new File("/sdcard/gift2.JPG"));
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
startActivityForResult(intent, 0);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0 && resultCode == Activity.RESULT_OK) { <br> Toast.makeText(getBaseContext(), "ImageCaptured",Toast.LENGTH_LONG).show();
Uri chosenImageUri = data.getData();
Bitmap mBitmap = null;
try {
mBitmap = Media.getBitmap(this.getContentResolver(),chosenImageUri);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ImageView img = new ImageView(this);
img.setImageBitmap(mBitmap);
setContentView(img);
}
}
}
当我执行这个课程时。通过相机捕获图像并单击“确定”后,我在语句
处获得空指针异常 "Uri chosenImageUri = data.getData();
“
答案 0 :(得分:0)
相机应用程序无法在2.2版本的模拟器中运行。尝试使用2.1或更低版本。工作正常。
答案 1 :(得分:0)
在图像视图上设置之前调整图像大小解决问题....
public Bitmap getResizedBitmap(Bitmap bm,int newHeight,int newWidth){
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
Matrix matrix = new Matrix();`enter code here`
// RESIZE THE BIT MAP
matrix.postScale(scaleWidth, scaleHeight);
// RECREATE THE NEW BITMAP
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height,
matrix, false);
return resizedBitmap;
}