title: Using ParcelFileDescriptor from URI for image selection in Android
body: <p>My app is creating an Intent using ACTION_GET_INTENT looking for image mime types. Upon returning I put together a long and elaborate process of checking the intent for data, getting a cursor from the content resolver of the URI, checking the uri.toString() and handling certain URI's different than others. For example "content://com.google.android.gallery3d" would be "content://com.google.android.apps.docs.storage" and so on.</p>
  
  <p>This was getting to be a pain, especially since KitKat came along and introduced more URI's to handle.</p>
  
  <p>BUT then I came along this piece of suggested code on some Google Developer pages related to SAF and KitKat. This seems to work perfectly. I tested in on KitKat, JB, and GB devices on all of the installed "file chooser" kind of apps. It never failed.</p>
  
  <p>**So my question is....is this code solid? Is there anything else to watch for?
  If this is the preferred method, why so many SO posts on how to deal with the returned Intent/URI from choosing images?</p>
  
  <p>The code sample was found at:
  <a href="https://developer.android.com/guide/topics/providers/document-provider.html" rel="noreferrer">https://developer.android.com/guide/topics/providers/document-provider.html</a>
  Look under the 'Bitmap' section.</p>
  
  <pre><code>try {
          final ParcelFileDescriptor parcelFileDescriptor = myContext.getContentResolver().openFileDescriptor(
                  imageUri, "r");
          final FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
          final Bitmap bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor);
          parcelFileDescriptor.close();
          return bitmap;
      } catch (Exception e) {
          Log.e(tag, "Failed to Parse Image Uri", e);
          throw new Exception("failed to parse image uri");
      }
  </code></pre>