title: BitmapFactory.decodeStream is returning null: Android
body: <p>I have a method in a second class (Class2) where I pass in the values of context and uri from Class1. The second class (Class2) is a class where filters will be applied to images taken using the camera from Class1. The method in Class2 looks something like this</p>
  
  <pre><code>public void prepareImage(Context context, Uri uri) {
  
      // BitmapFactory options
      Options options = new Options();
      options.inSampleSize = 2;
  
      String path = uri.toString();
      File file = new File(path);
  
          FileInputStream fis = null; //initialize
          try {
              fis = new FileInputStream(file);
          } catch (FileNotFoundException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
          Bitmap img = BitmapFactory.decodeStream(fis);
          Log.i(TAG, "Bitmap img: " + img);
                      //more code below, but above this comment is where the issue is
  
  }
  </code></pre>
  
  <p>I am noticing that FileInputStream is returning null, therefore BitmapFactory.decodeStream is decoding nothing. There are two things that I think are issues but am not sure how to address them. First, I was wondering if I am receiving null for FileInputStream because I am using a path that is formed from uri.toString(). Or does that matter? The string path value is correct.</p>
  
  <p>The second issue is that if my phone is tethered to the computer, after taking an image using my application, the image file does not seem to register until after I unplug the device from my computer. Only after I unplug my device from the computer does all my images come up in the gallery folder. So my suspicion is that file is not being found! There is evidence of this from logcat. </p>
  
  <p><img src="https://i.stack.imgur.com/O8KEt.jpg" alt="Note: I blocked out the name of the app"></p>
  
  <p>Anyway, I do not know how to get around this conflict. Maybe I need to save images in a different way. Here is how I am saving images in Class1.</p>
  
  <pre><code>File imgFileDir = getDir();
              if (!imgFileDir.exists() &amp;&amp; !imgFileDir.mkdirs()) {
                  Log.e(TAG, "Directory does not exist");
              }
  
          //Locale.US to get local formatting
          SimpleDateFormat timeFormat = new SimpleDateFormat("hhmmss", Locale.US);
          SimpleDateFormat dateFormat = new SimpleDateFormat("ddMMyyyy", Locale.US);
          String time = timeFormat.format(new Date());
          String date = dateFormat.format(new Date());
          String photoFile = date + "_nameofapp_" + time + ".jpg";       
          String filename = imgFileDir.getPath() + File.separator + photoFile;
  
          File pictureFile = new File(filename);
  
          try {
              FileOutputStream fos = new FileOutputStream(pictureFile);
              fos.write(arg0);
              fos.flush();
              fos.close();
          } catch (Exception e) {
              Log.e(TAG, "Image could not be saved");
              e.printStackTrace();
          }
  </code></pre>
  
  <p>I also use a helper method</p>
  
  <pre><code>private File getDir() {
          File sdDir = Environment.
                  getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
          return new File(sdDir, "NameOfApp");
      }
  </code></pre>
  
  <p>Lastly, I tried running the application untethered to my computer to see if it would work and it does not. The behavior is just the same, images that I take picture of does not show up in the gallery until I replug the device to my computer, and then all the images I took show up in the gallery. Why?</p>
  
  <p>Per request, I am passing the URI from Class1 doing the following.</p>
  
  <pre><code>Class2 classTwo = new Class2();
  classTwo.prepareImage(this, uri);
  </code></pre>