照片选择器。肖像转向风景

时间:2013-06-05 20:14:40

标签: android android-gallery android-image android-photos

How to pick an image from gallery (SD Card) for my app?

我已经实现了第二个答案(使用下采样)。当我以纵向选择图像时,图像将以横向模式显示。有人知道这是为什么吗?以及如何解决这个问题?提前谢谢!

P.S。对不起,我已经提出了一个新主题,但海报保护他的话题对抗像我这样的新手:)

1 个答案:

答案 0 :(得分:2)

你必须像这样获得图片的exif旋转并相应地安排你的位图

public static int getExifRotation(String imgPath) 
{
    try 
    {
        ExifInterface exif = new ExifInterface(imgPath);
        String rotationAmount = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
        if (!TextUtils.isEmpty(rotationAmount)) 
        {
            int rotationParam = Integer.parseInt(rotationAmount);
            switch (rotationParam) 
            {
                case ExifInterface.ORIENTATION_NORMAL:
                    return 0;
                case ExifInterface.ORIENTATION_ROTATE_90:
                    return 90;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    return 180;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    return 270;
                default:
                    return 0;
            }
        } 
        else 
        {
            return 0;
        }
    }
    catch (Exception ex) 
    {
        return 0;
    }
}

获取图片的路径

public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

精简制作矩阵并使用使用矩阵

的位图构造函数
Matrix matrix = new Matrix();
matrix.preRotate(90); 
// or
matrix.postRotate(90);

所以在你的onActivityResult中你应该有这样的东西

 Uri selectedImageUri = data.getData();

                selectedImagePath = getPath(selectedImageUri);
                orientation = getExifRotation(selectedImagePath);


                Matrix matrix = new Matrix();
                matrix.postRotate(90);
               if(orientation == 90){
                   bitmap = Bitmap.createBitmap(bitmap, 0, 0, 
                            bitmap.getWidth(), bitmap.getHeight(), 
                            matrix, true);}

请确保您首先对您的图片进行重新取样,以便他首先在答案中使用它然后执行此操作

相关问题