-1

I have an intent where the user selects an image from their gallery. I want to load this image into an ImageView using the Glide library. It seems like it can't handle the URI that is being passed into Glide

        addPhotos.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            ActivityCompat.requestPermissions(CreateEvacuationProcedureActivity.this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},1);

            Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
            intent.addCategory(Intent.CATEGORY_OPENABLE);
            intent.setType("image/*");
            startActivityForResult(intent, OPEN_DOCUMENT_CODE);
        }
    });



    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == OPEN_DOCUMENT_CODE && resultCode == RESULT_OK) {
        if (data != null) {
            // this is the image selected by the user
            Uri imageUri = data.getData();
            System.out.println(new File(imageUri.getPath()));


            Glide.with(this)
                    .load(imageUri.getPath()) // Uri of the picture
                    .listener(new RequestListener<Drawable>() {
                        @Override
                        public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                            System.out.println(e.toString());
                            return false;
                        }

                        @Override
                        public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                            return false;
                        }
                    })
                    .into(imageOne);
        }

    }
}

I am getting the following error in the console from Glide:

I/Glide: Root cause (1 of 2)
java.io.FileNotFoundException: /document/image:2442 (No such file or directory)
Zoe
  • 27,060
  • 21
  • 118
  • 148

2 Answers2

0

Because the uri that's returned is in the form /document/image:1234, Glide cannot correctly identify the path,need to convert it to an absolute path. you can try this!

String documentId = DocumentsContract.getDocumentId(uri);
if(isDocument(uri)){
    String id = documentId.split(":")[1];
    String selection = MediaStore.Images.Media._ID + "=?";
    String[] selectionArgs = {id};
    filePath = getDataColumn(context, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection, selectionArgs);
}

private boolean isDocument(Uri uri) {
    return "com.android.providers.media.documents".equals(uri.getAuthority());
}
Monk
  • 26
  • 2
0

Here is what I have done in Kotlin

Code to select image:

val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
startActivityForResult(intent, 9998)

Code to loadImage into ImageView using Glide.

        val filePathColumn = arrayOf(MediaStore.Images.Media.DATA)

        val cursor: Cursor? = context?.contentResolver?.query(
            backgroundUrl, filePathColumn, null, null, null
        )
        cursor?.moveToFirst()

        val columnIndex: Int? = cursor?.getColumnIndex(filePathColumn[0])
        val filePath: String? = columnIndex?.let { cursor?.getString(it) }
        if (cursor != null) {
            cursor.close()
        }


        context?.let {
            Glide.with(it)
                .load(File(filePath)).into(YOUR_IMAGE_VIEW_ID)
        }

Please also note that you will need to implement runtime permission (read/write external storage) for running your app on Android 6.0 Marshmallow (API 23) or later.

Pranav Choudhary
  • 2,726
  • 3
  • 18
  • 38