照片捕获意图只在三星手机上导致NullPointerException

照片捕获Intent只在三星手机上导致NullPointerException

下面的实施。

 final Button capture = (Button)findViewById(R.id.capture_button); capture.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST); } }); protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == CAMERA_PIC_REQUEST) { Bitmap thumbnail = (Bitmap)data.getExtras().get("data"); ImageView image = (ImageView)findViewById(R.id.photoResultView); image.setImageBitmap(thumbnail); } } 

Solutions Collecting From Web of "照片捕获意图只在三星手机上导致NullPointerException"

我find了一个解决scheme(不是我的工作),使其适用于三星设备。 有解释的博客可以在这里find。

但是,在非三星手机上使用此修补程序将返回错误的图像,所以我会使用一个

 if(imageURI != null) { // do it the normal way else { // do it the "Samsung" way } 

刚刚在Samsung S4上遇到同样的问题,发现在AndroidManifest.xml中添加configChanges解决了这个问题:

 <activity android:name=".YourActivity" android:configChanges="orientation|keyboardHidden|screenSize" android:screenOrientation="portrait" > </activity> 

你可以在这里查一下简单的方法来得到Uri。

在android中获取相机捕获图像path

调用相机

 Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(Intent.createChooser(cameraIntent,"Select Picture"), CAMERA_PIC_REQUEST1); 

结果活动

 final ContentResolver cr = getContentResolver(); final String[] p1 = new String[] { MediaStore.Images.ImageColumns._ID, MediaStore.Images.ImageColumns.DATE_TAKEN }; Cursor c1 = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, p1, null, null, p1[1] + " DESC"); if ( c1.moveToFirst() ) { String uristringpic = "content://media/external/images/media/" +c1.getInt(0); Uri newuri = Uri.parse(uristringpic); Log.i("TAG", "newuri "+newuri); } c1.close(); } 

然后你可以得到Uripath捕捉图像

在android中获取相机捕获图像path

在android中获取相机捕获图像path

调用相机

 Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(Intent.createChooser(cameraIntent,"Select Picture"), CAMERA_PIC_REQUEST1); 

结果活动

 final ContentResolver cr = getContentResolver(); final String[] p1 = new String[] { MediaStore.Images.ImageColumns._ID, MediaStore.Images.ImageColumns.DATE_TAKEN }; Cursor c1 = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, p1, null, null, p1[1] + " DESC"); if ( c1.moveToFirst() ) { String uristringpic = "content://media/external/images/media/" +c1.getInt(0); Uri newuri = Uri.parse(uristringpic); Log.i("TAG", "newuri "+newuri); } c1.close(); 

那么你可以得到Uripath捕捉图像

( 来源 )