title: Android Camera : Empty file in onActivityResult method
body: <p>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 <strong>one device</strong> I get null error on the <strong>file</strong> variable. </p>
  
  <p><em>"Attempt to invoke virtual method <strong>java.lang.String java.io.File.getPath()</strong> on a null object reference"</em></p>
  
  <p>This is the way how I implemented it.</p>
  
  <p><strong>A) Step one: Open camera</strong></p>
  
  <p>Upon clicking on a button I run the following code. This code will:</p>
  
  <ol>
  <li>Create a new file</li>
  <li><p>Open camera intent</p>
  
  <pre><code>    //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);
      }
  </code></pre></li>
  </ol>
  
  <p><strong>B) Step two: Take a picture and click save button</strong> </p>
  
  <p><strong>C) Step three: Grab the picture in onActivityResult() method</strong></p>
  
  <ol>
  <li><p>Check the constant = Constant.CAMERA_PIC_REQUEST.</p></li>
  <li><p>Get the EXIF data from the photo and find out the required angle to rotate the photo.</p></li>
  <li><p>Set the width, height and angle. Create the .jpg photo</p></li>
  <li><p>Save the created image name and path into imageArray</p></li>
  <li><p>Set the grid view with imageArray</p>
  
  <pre><code>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;
  
      }
  }
  }
  </code></pre></li>
  </ol>
  
  <p>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.</p>
  
  <p>Three devices that could run above code is having at least android v4.0 or above.
  The phone that gives me null exception is <strong>Samsung Galaxy Note 3 Android v5.0</strong>. (Although the code is failing, I could find the photo in the phone gallery)</p>
  
  <p>Can you please suggest me what could go wrong here? Am I missing something?
  Anyway to improve this code?</p>
  
  <p>Thanks!</p>