Photo capture Intent causes NullPointerException o

2020-01-29 05:08发布

问题:

Photo capture Intent causes NullPointerException on Samsung phones only.

Implementation below.

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);
    }
}

回答1:

I found a fix (not my work) that makes it work for Samsung devices. The blog with explanation can be found here.

However, using this fix on non-Samsung phones returns the wrong image, so I would use an

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


回答2:

Just got the same issue on a Samsung S4 and found out that adding configChanges to the AndroidManifest.xml solved the problem:

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


回答3:

you can check a little simple way over here to get Uri.

Get camera capture image path in android

calling camera

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

on result activity

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();
}

Then you can get Uri path capture image

Get camera capture image path in android



回答4:

Get camera capture image path in android

calling camera

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

on result activity

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();

then u can get Uri path capture image

(source)