使用 Glide 显示保存到外部存储 MediaStore 中的图像

Use Glide to display an image saved into external storage MediaStore

在上次 Android 11 update 之前,我曾经获取外部图像的文件路径以在 Glide 上显示它。由于上次更新,我现在需要使用 MediaStore 存储选项将我的图像存储在外部。我成功保存了我的图像,但我在检索它并在滑行中显示它时遇到问题。 我尝试使用 中的代码来获取 uri :

public Uri getUriFromContentResolver(String fileName) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
             ContentResolver resolver = context.getApplicationContext().getContentResolver();

            Cursor queryCursor = resolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Images.Media.DISPLAY_NAME,
                            MediaStore.Images.Media.RELATIVE_PATH}, MediaStore.Images.Media.DISPLAY_NAME + "=? ",
                    new String[]{fileName}, null);

            if (queryCursor != null && queryCursor.moveToFirst()) {

                return ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, queryCursor.getLong(0));
            } else {
                return null;
            }
        } else {
            return null;
        }
    }

并检索它以在滑动时显示它:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {

            Uri uriFromContentResolver = new ImageManager(context).getUriFromContentResolver(listPicture.get(pos).getFileName());

            if (uriFromContentResolver !=null) {
                System.out.println("Uri " + uriFromContentResolver.toString());

                Glide.with(context)
                        .load(uriFromContentResolver)
                        .placeholder(R.drawable.ic_baseline_person_24_black)
                        .into(pictureGallery);
            }

        } else {

            Glide.with(context)
                    .load(listPicture.get(pos).getPath())
                    .placeholder(R.drawable.ic_baseline_person_24_black)
                    .into(pictureGallery);
        }

我还测试了 glide 在使用 uri.getPath() 保存文件时加载路径:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                ContentResolver resolver = context.getContentResolver();
                ContentValues contentValues = new ContentValues();
                contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, fileName);
                contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/jpg");
                contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES);
                Uri imageUri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
                fos = resolver.openOutputStream(Objects.requireNonNull(imageUri));
               

                String path = imageUri.getPath();

我不习惯MediaStore系统,我会采纳任何建议。谢谢。

我创建了一个class通过文件名

获取文件的Uri
Uri uriFromContentResolver = getUriFromContentResolver(fileName);
public Uri getUriFromContentResolver(String fileName) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            ContentResolver resolver = context.getApplicationContext().getContentResolver();

            Cursor queryCursor = resolver.query(
                    MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL),
                    new String[]{
                            MediaStore.Images.Media.DISPLAY_NAME,
                            MediaStore.Images.Media.RELATIVE_PATH},
                    MediaStore.Images.Media.DISPLAY_NAME + " = ?",
                    new String[]{fileName}, null);

            if (queryCursor != null && queryCursor.moveToFirst())
            {
                return ContentUris.withAppendedId(
                        MediaStore.Images.Media.getContentUri(
                                MediaStore.VOLUME_EXTERNAL),
                        queryCursor.getLong(0));
            } else
            {
                return null;
            }

        } else {
            return null;
        }
    }

一旦我们有了它,我们就用我们刚刚获得路径的 Uri 调用这个函数

String path = getRealPathFromURI(uriFromContentResolver);
public String getRealPathFromURI(Uri contentUri) {
        String[] proj = { MediaStore.Images.Media.DATA };
        CursorLoader loader = new CursorLoader(context, contentUri, proj, null, null, null);
        Cursor cursor = loader.loadInBackground();
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        String result = cursor.getString(column_index);
        cursor.close();
        return result;
    }

并且通过这个路径,Glide 可以读取文件

Glide.with(context)
         .load(path)
         .placeholder(R.drawable.ic_baseline_person_24_black)
         .into(pictureGallery);