title: Android scaling down image from gallery with Uri and Bitmap
body: <p>I am receiving a FileNotFound exception when trying to turn an image into a Bitmap to scale the image down significantly.</p>
  
  <p>I am retrieving an image the user selects from the gallery like so</p>
  
  <pre><code>public static void GET_PICTURE_FROM_GALLERY (Activity activity)
  {
      Intent getIntent = new Intent(Intent.ACTION_GET_CONTENT);
      getIntent.setType("image/*");
  
      Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
      pickIntent.setType("image/*");
  
      Intent chooserIntent = Intent.createChooser(getIntent, "Select Image");
      chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {pickIntent});
  
      activity.startActivityForResult(chooserIntent, REQUEST_SELECT_PHOTO);
  }
  </code></pre>
  
  <p>Then, when the user is done picking: </p>
  
  <pre><code>public void setChosenPicture (Activity activity, Intent data)
  {
      if(editingViewHelper != null)
      {
          String[] filePathColumn = {MediaStore.Images.Media.DATA};
  
          Uri selectedImage = data.getData();
  
          System.out.println("GOT URI: " + selectedImage.toString());
  
          Cursor cursor = activity.getContentResolver().query(selectedImage, filePathColumn, null, null, null);
          cursor.moveToFirst();
  
          int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
          editingViewHelper.holder.tempUri = Uri.parse(cursor.getString(columnIndex));
  
          System.out.println("SET URI: " + editingViewHelper.holder.tempUri.toString());
  
          cursor.close();
  
          editingViewHelper.holder.imageView.setImageURI(editingViewHelper.holder.tempUri);
      }
  }
  </code></pre>
  
  <p>This outputs the following:</p>
  
  <pre><code>GOT URI: content://media/external/images/media/110
  SET URI: /storage/emulated/0/DIRECTORY/IMG_20170606_154512_1664512804.jpg
  </code></pre>
  
  <p>Now this works great, but only if I call imageView.setImageUri(uri); However, I cannot do that because I must scale the image down so the app doesn't use ridiculous amounts of ram. To do this, I have this method</p>
  
  <pre><code>public static Bitmap decodeUri(Context c, Uri uri, final int requiredSize)
  {
      System.out.println("URI: " + uri.toString());
  
      try
      {
          BitmapFactory.Options o = new BitmapFactory.Options();
          o.inJustDecodeBounds = true;
  
   //This line is where the error occurs. 
       BitmapFactory.decodeStream(c.getContentResolver().openInputStream(uri), null, o);  
  
          int width_tmp = o.outWidth;
          int height_tmp = o.outHeight;
          int scale = 1;
  
          while(true)
          {
              if(width_tmp / 2 &lt; requiredSize || height_tmp / 2 &lt; requiredSize)
                  break;
  
              width_tmp /= 2;
              height_tmp /= 2;
              scale *= 2;
          }
  
          BitmapFactory.Options o2 = new BitmapFactory.Options();
          o2.inSampleSize = scale;
  
          return BitmapFactory.decodeStream(c.getContentResolver().openInputStream(uri), null, o2);
      }
      catch (FileNotFoundException e)
      {
          e.printStackTrace();
          return null;
      }
  }
  </code></pre>
  
  <p>And finally, I have the following .output FileNotFound exception</p>
  
  <pre><code>URI: /storage/emulated/0/DIRECTORY/IMG_20170606_154512_1664512804.jpg
  java.io.FileNotFoundException: No content provider: /storage/emulated/0/DIRECTORY/IMG_20170606_154512_1664512804.jpg
  </code></pre>
  
  <p>Now the weird thing is that this method <em>does</em> work but only if it is passed the Uri from a picture <em>taken</em> in my app. Here is an example output of the last method that does not throw any errors</p>
  
  <pre><code>Uri: content://com.subliroid.dinnerdecider.provider/external_files/Pictures/DIRECTORY/IMG_20170606_154512_1664512804.jpg
  </code></pre>
  
  <p>I am able to call imageView.setImageUri(uri) with BOTH types of uris, but I cannot scale the one type down. I was able to ignore this until I began testing my app on real devices that take 4k+ pictures compared to the small ones the emulator takes. </p>