如何检索缩略图旋转

use*_*676 6 android picasso

我正在创建一种"图库"应用程序,它在网格中显示所有图像.

问题是: 某些图像没有以正确的方向显示.

这是检索缩略图的代码

final String[] projection = { MediaStore.Images.Thumbnails.DATA, MediaStore.Images.Thumbnails.IMAGE_ID };
//query the thumbnails provider
Cursor thumbnailsCursor = context.getContentResolver().query(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection, null,
            null, null);
if (thumbnailsCursor.moveToFirst()) {
        do {
            //get the thumbnail path
            fullPath = thumbnailsCursor.getString(fullPathColumnIndex);
            thumbnailUri = Uri.parse(fullPath);
            //add the uri to the list
            thumbnailsList.add(thumbnailUri);
}  while (thumbnailsCursor.moveToNext());
thumbnailsCursor.close();
Run Code Online (Sandbox Code Playgroud)

里面getView()的的BaseAdapter我使用的Picasso图像加载器库显示缩略图,但有时方向是错的.

Picasso.with(context).load(new File(photoItem.thumbnail.getPath())).noFade().into(holder.photoImageView);
Run Code Online (Sandbox Code Playgroud)

我已经尝试查询真实的照片数据并检索方向,但过程太慢(几秒钟)并且显示的图像太大.

nyx*_*nyx 13

给定图像的ID,您可以查询MediaStore以获取原始图像的路径,然后从那里获取方向的exif.

这个过程非常快,通常只需要几毫秒.

 Cursor cursor =
          CustomApplication
              .getContext()
              .getContentResolver()
              .query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                  new String[] {MediaStore.Images.Media.DATA}, MediaStore.Images.Media._ID + "=?",
                  new String[] {"" + PHOTO_ID}, null);
      if (cursor != null && cursor.getCount() > 0) {
        cursor.moveToFirst();
        String fullPath = cursor.getString(0);
        cursor.close();
        ExifInterface exif = new ExifInterface(fullPath);
        int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
        //ROTATE AND FLIP YOUR THUMBNAIL AS NEEDED BASED ON exifOrientation
      }
Run Code Online (Sandbox Code Playgroud)


Mic*_*ael 1

您可以按如下方式使用 ExifInterface 类:

int originalOrientation = ExifInterface.ORIENTATION_NORMAL;

try {
    ExifInterface exif = new ExifInterface(photoItem.thumbnail.getPath().toString());
    originalOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
} catch (IOException e) {
    e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)

然后您可以检查它是否旋转并执行您需要的任何逻辑(例如向毕加索请求传递旋转角度)。

if (originalOrientation == ExifInterface.ORIENTATION_ROTATE_90 || originalOrientation == ExifInterface.ORIENTATION_ROTATE_270) {
    //do something
}
Run Code Online (Sandbox Code Playgroud)

这是我使用的方法,我从未注意到任何性能影响。