在 android 7.1 上使用 Picasso 从 MediaStore 加载图像失败
loading images from MediaStore fails with Picasso on android 7.1
您好,我正在像这样从 android MediaStore 将图像加载到网格
private ArrayList<ImageItem> loadImageData() {
Cursor cursor;
int columnIndex;
// Set up an array of the Thumbnail Image ID column we want
String[] projection = {MediaStore.Images.Media.DISPLAY_NAME, MediaStore.Images.Thumbnails._ID, MediaStore.Images.Media.DATA, MediaStore.Images.Thumbnails.DATA};
// Create the cursor pointing to the SDCard
cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection, // Which columns to return
null, // Return all rows
null,
null);
// Get the column index of the Thumbnails Image ID
int imageID;
cursor.moveToFirst();
final ArrayList<ImageItem> imageItems = new ArrayList<>();
for (int i = 0; i < cursor.getCount(); i++) {
columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
imageID = cursor.getInt(columnIndex);
int nameIndex = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME);
int fullImageIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
String uri = "file://" + cursor.getString(fullImageIndex);
int thumbIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA);
String TbUri = "file://" + cursor.getString(thumbIndex);
imageItems.add(new ImageItem(TbUri, cursor.getString(nameIndex), uri));
cursor.moveToNext();
}
cursor.close();
return imageItems;
}
然后我将拇指添加到网格
Picasso.with(gridContext).load(item.getImage()).fit().centerCrop().into(Holder.image);
在 android 4.0.1 设备上此代码工作正常。
但是在 android 7.1 上,我加载到网格的图像很少,其余的都丢失了。
未显示的图像是使用设备相机拍摄的 JPEG。来自下载文件夹或 whatsApp 的图片显示正确。
在 logcat 中出现以下错误:
e: Invalid image: ExifInterface got an unsupported image format file(ExifInterface supports JPEG and some RAW image formats only) or a corrupted JPEG file to ExifInterface. java.io.IOException: Invalid marker: 89
这可能与缩略图有关吗?因为我使用:
int thumbIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA);
String TbUri = "file://" + cursor.getString(thumbIndex);
降级到 Picasso 版本 2.4.0 似乎可以解决问题。也可能与设备有关。我的设备是 onePlus 3 7.1.1 希望这对其他人有帮助。
您好,我正在像这样从 android MediaStore 将图像加载到网格
private ArrayList<ImageItem> loadImageData() {
Cursor cursor;
int columnIndex;
// Set up an array of the Thumbnail Image ID column we want
String[] projection = {MediaStore.Images.Media.DISPLAY_NAME, MediaStore.Images.Thumbnails._ID, MediaStore.Images.Media.DATA, MediaStore.Images.Thumbnails.DATA};
// Create the cursor pointing to the SDCard
cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection, // Which columns to return
null, // Return all rows
null,
null);
// Get the column index of the Thumbnails Image ID
int imageID;
cursor.moveToFirst();
final ArrayList<ImageItem> imageItems = new ArrayList<>();
for (int i = 0; i < cursor.getCount(); i++) {
columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
imageID = cursor.getInt(columnIndex);
int nameIndex = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME);
int fullImageIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
String uri = "file://" + cursor.getString(fullImageIndex);
int thumbIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA);
String TbUri = "file://" + cursor.getString(thumbIndex);
imageItems.add(new ImageItem(TbUri, cursor.getString(nameIndex), uri));
cursor.moveToNext();
}
cursor.close();
return imageItems;
}
然后我将拇指添加到网格
Picasso.with(gridContext).load(item.getImage()).fit().centerCrop().into(Holder.image);
在 android 4.0.1 设备上此代码工作正常。
但是在 android 7.1 上,我加载到网格的图像很少,其余的都丢失了。 未显示的图像是使用设备相机拍摄的 JPEG。来自下载文件夹或 whatsApp 的图片显示正确。
在 logcat 中出现以下错误:
e: Invalid image: ExifInterface got an unsupported image format file(ExifInterface supports JPEG and some RAW image formats only) or a corrupted JPEG file to ExifInterface. java.io.IOException: Invalid marker: 89
这可能与缩略图有关吗?因为我使用:
int thumbIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA);
String TbUri = "file://" + cursor.getString(thumbIndex);
降级到 Picasso 版本 2.4.0 似乎可以解决问题。也可能与设备有关。我的设备是 onePlus 3 7.1.1 希望这对其他人有帮助。