如何检索缩略图旋转

时间:2014-09-16 13:22:24

标签: 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();

getView()的{​​{1}}内我使用BaseAdapter图像加载程序库来显示缩略图,但有时方向错误。

Picasso

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

5 个答案:

答案 0 :(得分: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
      }

答案 1 :(得分:2)

这是正确的解决方案。您可以从MediaStore查询方向数据,以便使用 MediaStore.Images.Media.ORIENTATION获得入学学位。

 final String[] projection = { MediaStore.Images.Thumbnails.DATA, MediaStore.Images.Thumbnails.IMAGE_ID, MediaStore.Images.Media.ORIENTATION };

 Cursor thumbnailsCursor = context.getContentResolver().query(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection, null, null, null);
if (thumbnailsCursor.moveToFirst()) {
do {
  int orientationColumnIndex    =   thumbnailsCursor.getColumnIndex(MediaStore.Images.Media.ORIENTATION);
  int orientation = cur.getInt(orientationColumnIndex);
//do what you want to do with orientation value
}while (thumbnailsCursor.moveToNext());
 thumbnailsCursor.close();

会以度数为您提供方向。

答案 2 :(得分: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();
}

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

if (originalOrientation == ExifInterface.ORIENTATION_ROTATE_90 || originalOrientation == ExifInterface.ORIENTATION_ROTATE_270) {
    //do something
}

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

答案 3 :(得分:1)

我发现你可以在picasso lib中为你的加载任务应用自定义转换。

这里是solution引用它。

那里的转换方法正在使用 MATRIX ,就像其他人给出的那样......

我还没有检查过。

答案 4 :(得分:-1)

public static float getOrientationValue(String location) {
    ExifInterface exif = null;
    try {
        exif = new ExifInterface(new File(location).getAbsolutePath());

    } catch (IOException e) {
        e.printStackTrace();
    }
    int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
        ExifInterface.ORIENTATION_NORMAL);
    int rotate = 0;
    switch (orientation) {
    case ExifInterface.ORIENTATION_ROTATE_270:
        rotate += 90;
    case ExifInterface.ORIENTATION_ROTATE_180:
        rotate += 90;
    case ExifInterface.ORIENTATION_ROTATE_90:
        rotate += 90;
    }
    return rotate;
    }

  public static int getOrientation(Context context, Uri photoUri) {
        /* it's on the external media. */
        Cursor cursor = context.getContentResolver().query(photoUri,
                new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null);

        if (cursor.getCount() != 1) {
            return -1;
        }

        cursor.moveToFirst();
        return cursor.getInt(0);
    }
相关问题