【发布时间】:2021-09-24 04:15:37
【问题描述】:
在上次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 系统,我会接受任何建议。谢谢。
【问题讨论】:
标签: file storage android-glide mediastore android-11