如何解决使用 Glide 显示保存在外部存储 MediaStore 中的图像
在上个 Android 11 update 之前,我曾经通过获取我的外部图像的文件路径将其显示在 Glide 上。由于上次更新,我现在需要使用 MediaStore 存储选项将我的图像存储在外部。我成功保存了我的图像,但在检索它并在滑行上显示它时遇到了问题。 我尝试使用 this question 中的代码来获取 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 系统,我会接受任何建议。谢谢。
解决方法
我创建了一个类来使用文件名获取文件的 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);
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);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。