Photo capture Intent causes NullPointerException on Samsung phones only
Asked Answered
S

4

11

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);
    }
}
Swept answered 11/8, 2011 at 19:3 Comment(1)
@Matt the image.setImageBitmap(thumbnail); line as thumbnail was nullEphedrine
G
13

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
}
Gilliland answered 11/8, 2011 at 19:6 Comment(1)
as @Pyrodante suggested "However, using this fix on non-Samsung phones returns the wrong image, so I would use an " could any one please post the complete code , which should work on both samsung and non samsung'sMastaba
C
4

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>
Crocket answered 4/12, 2015 at 17:21 Comment(1)
Works for me! Weird Samsung!Variation
G
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

Gens answered 18/1, 2012 at 8:35 Comment(0)
E
-2

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)

Eec answered 17/9, 2012 at 12:26 Comment(1)
Lone link is considered a poor answer since it is meaningless by itself and target resource is not guaranteed to be alive in the future. Please try to include at least summary of information you are linking to.Serafinaserafine

© 2022 - 2024 — McMap. All rights reserved.