0

I have downloaded an image from Google Drive storage. I want to set this image to imageview in my app using Gallery Intent.

Uri of image received in onActivityResult: content://com.google.android.apps.docs.storage/document/acc%3D2%3Bdoc%3Dencoded%3DWe%2BdXDcNgTeulVk1Ntu3YfRYkm9wk7uTdTG6LFVyck1BxY4g7xAxyPAgtMtz4A%3D%3S

I am using this code get the real-path from the url:

 if ("com.google.android.apps.docs.storage".equals(uri
                    .getAuthority())) {

    //content://com.google.android.apps.docs.storage/document/acc%3D2%3Bdoc%3Dencoded%3DWe%2BdXDcNbYeulVk1Ntu3YfRYkm9wk7uTdTG6LE6yck1BxY4g7xAxyPAgtMtz4A%3D%3D

 final String docId = DocumentsContract.getDocumentId(uri);
                    final String id = docId.split(";")[1];
                    final String[] selectionArgs = new String[]{id};
                    //String[] column = {MediaStore.Images.Media.DATA};


                    // where id is equal to
                    String selection = MediaStore.Images.Media._ID + "=?";
                    Uri contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

                    //..
                    Cursor cursor = null;
                    String filePath = null;
                    try {
                        String[] column = {MediaStore.Images.Media.DATA};

                        cursor = context.getContentResolver().query(contentUri,
                                column, selection, selectionArgs, null);
                        if (cursor != null && cursor.moveToFirst()) {
                            int columnIndex = cursor.getColumnIndex(column[0]);
                            filePath = cursor.getString(columnIndex);
                        }
                    } finally {
                        if (cursor != null)
                            cursor.close();

                        return filePath;


 }
            }

.....

But I am getting NULL cursor. Thus, filepath is always empty/null.

Anyone, could please suggest what I am doing wrong?

pravingaikwad07
  • 482
  • 1
  • 7
  • 24

1 Answers1

0

I am using this code get the real-path from the url

Delete it.

Anyone, could please suggest what I am doing wrong?

You are using that code.

Pass the Uri to your favorite image-loading library (Glide, Picasso, etc.). It will do all the work for you, populating your ImageView, in just a few lines of code.

Or, if you insist on doing it yourself:

  • Use a ContentResolver and openInputStream() to get an InputStream on the content identified by the Uri
  • Use BitmapFactory.decodeStream() to get a Bitmap based on that InputStream
  • Do those two steps on a background thread, as you should not do disk I/O and image decoding on the main application thread
  • On the main application thread, put the Bitmap in your ImageView,
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491