Friday, 14 December 2012

Photo capture Intent causes NullPointerException on Samsung phones only

Hello Dorid Guys,

Yesterday, I came across  a weird situation . In my application ,I am capturing an image from camera and displaying it inside an imageview. I tested its functionality on many of android device and its working 
fine except "Samsung Galaxy Tab".
In Samsung device the capture intent will be "Null" and I am getting null pointer exception while capturing image from camera in android Samsung S2.

Following code I am using which running fine in all devices except Samsung :

For capturing Image from camera:

Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(captureIntent, CAMERA_CAPTURE);

For choosing an image from gallery:

Intent gallaryIntent = new Intent(Intent.ACTION_PICK,
startActivityForResult(gallaryIntent, RESULT_LOAD_IMAGE);

And finally the onActivityResult(int requestCode, int resultCode, Intent data) :

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
       if (requestCode == RESULT_LOAD_IMAGE) {
         picUri = data.getData();

       if (requestCode == CAMERA_CAPTURE) {
  // get the Uri for the captured image
  picUri = data.getData();
  if (picUri != null) {
 // user is returning from cropping the image
  if (requestCode == PIC_CROP) {
  // get the returned data
  Bundle extras = data.getExtras();
  // get the cropped bitmap
  rectangleBitmap = extras.getParcelable("data");
  // retrieve a reference to the ImageView
  GraphicsUtil graphicsUtil = new GraphicsUtil();
  rectangleBitmap = graphicsUtil.getRoundedCornerBitmap(
  rectangleBitmap, 16);
  ImageView picView = (ImageView) findViewById(;
  // display the returned cropped image

Now the crop image method, picking the image from gallery or camera and crop it.

private void performCrop() {
 // take care of exceptions
 try {
 // call the standard crop action intent (the user device may not support it)
 Intent cropIntent = new Intent("");
 // indicate image type and Uri
 cropIntent.setDataAndType(picUri, "image/*");
 // set crop properties
 cropIntent.putExtra("crop", "true");
 // indicate aspect of desired crop
 cropIntent.putExtra("aspectX", 1);
 cropIntent.putExtra("aspectY", 1);
 // indicate output X and Y
 cropIntent.putExtra("outputX", 256);
 cropIntent.putExtra("outputY", 256);
 // retrieve data on return
 cropIntent.putExtra("return-data", true);
 // start the activity - we handle returning in onActivityResult
 startActivityForResult(cropIntent, PIC_CROP);
 }catch (ActivityNotFoundException anfe) {
  // display an error message
  String errorMessage = "Whoops - your device doesn't support the 
                   crop action!";
  Toast toast = Toast.makeText(this, errorMessage,

The above code is working fine in most of the devices but when test it on Samsung S2 . I am getting the NullPointerException.
This is very tough time for me because my client wants to release the Application ASAP. And because of this issue he is delaying its time. 

After spending a full day and a night a found the fix for this problem, I added some extra code inside the
onActivityResult method.

The trick is:

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

And it works :)
Here is my source code:

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
   super.onActivityResult(requestCode, resultCode, data);  
  if (resultCode == RESULT_OK) {  
  if (requestCode == RESULT_LOAD_IMAGE) {  
    picUri = data.getData();  
  if (requestCode == CAMERA_CAPTURE) {  
  // get the Uri for the captured image  
  picUri = data.getData();  
  * In samsung , the picUri will be null and application will  
  * give runtime error In else part we are doing the code for  
  * samsung device  
  if (picUri != null) {  
    // do it the normal way  
  } else {  
   // Describe the columns you'd like to have returned.  
   // Selecting from the Thumbnails location gives you both the  
   // Thumbnail Image ID, as well as the original image ID  
   String[] projection = {  
     MediaStore.Images.Thumbnails._ID, // The columns we wANT  
  MediaStore.Images.Thumbnails.DATA };  
   String selection = MediaStore.Images.Thumbnails.KIND + "=" + // Select  
                // only        // mini's  
      String sort = MediaStore.Images.Thumbnails._ID + " DESC";  
      // At the moment, this is a bit of a hack, as I'm returning  
   // ALL images, and just taking the latest one. There is a  
   // better way to narrow this down I think with a WHERE  
   // clause which is currently the selection variable  
      Cursor myCursor = this.managedQuery(  
   projection, selection, null, sort);  
   long imageId = 0l;  
   long thumbnailImageId = 0l;  
   String thumbnailPath = "";  
   try {  
     imageId = myCursor.getLong(myCursor   .getColumnIndexOrThrow(MediaStore.Images.Thumbnails.IMAGE_ID));  
         thumbnailImageId = myCursor.getLong(myCursor   .getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID));  
     thumbnailPath = myCursor  
  .getString(myCursor    .getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA));  
   } finally {  
  // Create new Cursor to obtain the file Path for the large  
  // image  
  String[] largeFileProjection = {  
   MediaStore.Images.ImageColumns.DATA };  
  String largeFileSort = MediaStore.Images.ImageColumns._ID  
     + " DESC";  
  myCursor = this.managedQuery(  
   largeFileProjection, null, null, largeFileSort);  
   String largeImagePath = "";  
  try {  
       // This will actually give yo uthe file path location of  
    // the image.  
    largeImagePath = myCursor  
  } finally {  
  // These are the two URI's you'll be interested in. They  
  // give you a handle to the actual images  
  Uri uriLargeImage = Uri.withAppendedPath(  
  Uri uriThumbnailImage = Uri.withAppendedPath(  
  picUri = uriLargeImage;  

Hope this will helps Some one.
Enjoy Coding :)

Mukesh Yadav

Hi Guys I am from Delhi working as Web/Mobile Application Developer(Android Developer), also have knowledge of Roboelctric and Mockito ,android test driven development... Blogging has been my passion and I think blogging is one of the powerful medium to share knowledge and ideas....


  1. Nice! hope it will work fine on my client device..

  2. Thanks Manish,
    I believe, its worked fine on your client device.

  3. first, thank you, that you published your solution :D

    But the constant RESULT_LOAD_IMAGE CAMERA_CAPTURE PIC_CROP doesn't exist.

  4. Pleas tell me what i have to import to get this constants

  5. Hello ,
    This is nothing only the request code, which I assigned as constant inside my class. like this...

    public class ImageCapture extends Activity {
    final int CAMERA_CAPTURE = 1;
    final int PIC_CROP = 2;
    private static int RESULT_LOAD_IMAGE =3;

    public void onCreate(Bundle
    savedInstanceState) {
    //you camera and gallery code

    //Here put the above onActivityResult code

    Hope, Now you got it...

  6. Ok thank you.

    One more Question: where is the best place to put the Cod (to view the image in an Image View).

    ImageView image = (ImageView) findViewById(;
    Bitmap myBitmap = BitmapFactory.decodeFile(path);
    Bitmap mySmalBitmap = Bitmap.createScaledBitmap(myBitmap, 250, 325,

    (path must be replaced through the real path :D)

  7. First of all let me know if you are using the cropping in your code , after picking the image from the gallery or camera ??

    The same way as I show in above code?

  8. sorry for the stupid question, but i don't know the function of "crop"

  9. Okay, Its mean you ate not cropping the pictuer.
    Then, in this case best way is to place your code inside onActivityResult().Also add null check code for image path and bitmap.

  10. so i call my method when you call performCrop();

  11. Right, and also remember to pass the parameter "path" in that function.

  12. Ok Thank you again.

  13. hi thanks for your coding....But in my samsung galaxy y mobile the core option not working..i.e cropping view and save option every thing working but after crop the gallery image not cropped's showing the as usel image

  14. Hello kumaravel,

    I have tested the above code samsung S2,s3, galaxy duaos and htc and It works fine for me on all this devices. May be something is going wrong at your end...if you provide me your cropping code then I will help you.

  15. Hello.. I wanna ask you..

    Where is the best way to put the code to view the image in ImageView? after doing the cropping thing.

    Thanks before, your code really work (even for samsung)

  16. If you are doing the cropping then I think its good to place the code after the cropping I did above because the cropping will work on both case either taking the picture from camera or by gallery.So making this a separate method and called startActivityForResult()...

    Hope you understand it.

  17. Thanks for this solution... I wonder that no one has provided the solution.. And whats the exact problem with Samsung? why it gives null??

    I will try your solution and will get back to you if there's any problem. Thanks again :)

  18. Hey mukesh, Nokia X (Android)having same there any alternative for that.

  19. Hello Deependra.
    check this link for alternative solution:

  20. Hi mukesh I am not getting Uri via picUri = data.getData();
    Any solution of that?

  21. Hi mukesh i am not getting Uri in picUri = data.getData();
    Any solution for that?



Copyright @ 2013 Android Developer Blog.

Blog Widget by LinkWithin