3
votes

I have an activity to open camera intent and grab the photo in the onActivityResult method. Three devices worked well without errors but only in one device I get null error on the file variable.

"Attempt to invoke virtual method java.lang.String java.io.File.getPath() on a null object reference"

This is the way how I implemented it.

A) Step one: Open camera

Upon clicking on a button I run the following code. This code will:

  1. Create a new file
  2. Open camera intent

        //create file
        imgName = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()) + ".jpg";
        //global variable
        file = new File(Environment.getExternalStorageDirectory(), imgName);
    
        if(file != null){
           //save image here
    
           Uri relativePath = Uri.fromFile(file);
    
           //Open camera
           Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
           intent.putExtra(MediaStore.EXTRA_OUTPUT, relativePath);
           startActivityForResult(intent, Constant.CAMERA_PIC_REQUEST);
        }
    

B) Step two: Take a picture and click save button

C) Step three: Grab the picture in onActivityResult() method

  1. Check the constant = Constant.CAMERA_PIC_REQUEST.

  2. Get the EXIF data from the photo and find out the required angle to rotate the photo.

  3. Set the width, height and angle. Create the .jpg photo

  4. Save the created image name and path into imageArray

  5. Set the grid view with imageArray

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    
    
    if (resultCode == RESULT_OK) {
    
        switch (requestCode) {
    
        case Constant.CAMERA_PIC_REQUEST:
            try {
    
                //Get EXIF data and rotate photo (1. file.getPath() has been used)
                ExifInterface exif = new ExifInterface(file.getPath());
                int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
    
                int angle = 0;// Landscape
    
                //rotate photo
                if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
                    angle = 90;
                } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
                    angle = 180;
                } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
                    angle = 270;
                }
    
                Matrix matrix = new Matrix();
                matrix.postRotate(angle);
    
    
                try {
    
                    //Create the rotated .jpg file (2. file.getPath() has been used)
                    Bitmap correctBmp = Func.decodeSampleBitmapFromFile(file.getPath(), Constant.PHOTO_WIDTH, Constant.PHOTO_HEIGHT);
                    correctBmp = Bitmap.createBitmap(correctBmp, 0, 0, correctBmp.getWidth(), correctBmp.getHeight(), matrix, true);
    
                    FileOutputStream fOut;
    
                    fOut = new FileOutputStream(file);
                    correctBmp.compress(Bitmap.CompressFormat.JPEG, 80, fOut);
                    fOut.flush();
                    fOut.close();
    
                    //image saved successfully - add photo name into reference array. Later I will use this
                    //(3. file.getPath() has been used)
                    Image img = new Image();
                    img.setImageName(imgName);
                    img.setAbsolutePath(file.getAbsolutePath());
                    img.setRelativePath(Uri.fromFile(file));
                    imageArray.add(img);//save in array
    
    
                } catch (FileNotFoundException e) {
                    Log.e("Error", e.getMessage());
    
                } catch (IOException e) {
                    Log.e("Error", e.getMessage());
    
                }
    
            } catch (Exception e) {
                //This exception is triggering.
                Log.e("Error 15", e.getMessage());
            }
    
            //set this photo in a grid view for user to see
            setGridView(imageArray);
            break;
    
        case Constant.POPUPFRAG:
            //something else
            break;
        default:
            //deafult
            break;
    
        }
    }
    }
    

Error 15 (third exception) is triggering only in one phone. I'm not sure why is that. It shows the null pointer exception that I posted above.

Three devices that could run above code is having at least android v4.0 or above. The phone that gives me null exception is Samsung Galaxy Note 3 Android v5.0. (Although the code is failing, I could find the photo in the phone gallery)

Can you please suggest me what could go wrong here? Am I missing something? Anyway to improve this code?

Thanks!

1

1 Answers

1
votes

Instead of directly using file.getPath() you can get use like this.

Create Global varibale of Uri then use it in all activity.

private Uri imageToUploadUri;
file = new File(Environment.getExternalStorageDirectory(), imgName);

if(file != null){
   //save image here

   imageToUploadUri = Uri.fromFile(file);

   //Open camera
   Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
   intent.putExtra(MediaStore.EXTRA_OUTPUT, imageToUploadUri);
   startActivityForResult(intent, Constant.CAMERA_PIC_REQUEST);
}

then in your onActivityResult() use like this:

public void onActivityResult(int requestCode, int resultCode, Intent data) {


if (resultCode == RESULT_OK) {

    switch (requestCode) {

    case Constant.CAMERA_PIC_REQUEST:
        try {


            ExifInterface exif = new ExifInterface(imageToUploadUri.getPath());
            int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

            int angle = 0;// Landscape

            //rotate photo
            if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
                angle = 90;
            } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
                angle = 180;
            } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
                angle = 270;
            }

            Matrix matrix = new Matrix();
            matrix.postRotate(angle);


            try {

                //Create the rotated .jpg file (2. file.getPath() has been used)
                Bitmap correctBmp = Func.decodeSampleBitmapFromFile(imageToUploadUri.getPath(), Constant.PHOTO_WIDTH, Constant.PHOTO_HEIGHT);
                correctBmp = Bitmap.createBitmap(correctBmp, 0, 0, correctBmp.getWidth(), correctBmp.getHeight(), matrix, true);

                FileOutputStream fOut;

                fOut = new FileOutputStream(file);
                correctBmp.compress(Bitmap.CompressFormat.JPEG, 80, fOut);
                fOut.flush();
                fOut.close();

                //image saved successfully - add photo name into reference array. Later I will use this
                //(3. file.getPath() has been used)
                Image img = new Image();
                img.setImageName(imgName);
                img.setAbsolutePath(file.getAbsolutePath());
                img.setRelativePath(imageToUploadUri);
                imageArray.add(img);//save in array


            } catch (FileNotFoundException e) {
                Log.e("Error", e.getMessage());

            } catch (IOException e) {
                Log.e("Error", e.getMessage());

            }

        } catch (Exception e) {
            //This exception is triggering.
            Log.e("Error 15", e.getMessage());
        }

        //set this photo in a grid view for user to see
        setGridView(imageArray);
        break;

    case Constant.POPUPFRAG:
        //something else
        break;
    default:
        //deafult
        break;

    }
}
}

I hope it helps you!