通过Glide将图库中的图像加载到ImageView中

时间:2018-12-25 23:16:54

标签: android imageview uri android-glide

我有一个意图,用户可以从画廊中选择图像。我想使用Glide库将此图像加载到ImageView中。似乎无法处理传递给Glide的URI

        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);
        }

    }
}

我从Glide的控制台中收到以下错误:

I/Glide: Root cause (1 of 2)
java.io.FileNotFoundException: /document/image:2442 (No such file or directory)

2 个答案:

答案 0 :(得分:0)

由于返回的uri格式为/ document / image:1234, Glide无法正确识别路径,需要将其转换为绝对路径。 你可以试试看!

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());
}

答案 1 :(得分:0)

这是我在 Kotlin 中所做的

选择图片的代码:

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

使用 Glide 将图像加载到 ImageView 的代码。

        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)
        }

另请注意,您需要实现运行时权限(读/写外部存储)才能在 Android 6.0 Marshmallow (API 23) 或更高版本上运行您的应用。