【问题标题】:Gallery images get rotated when saved保存时图库图像会旋转
【发布时间】:2017-09-21 13:23:11
【问题描述】:

我一直在尝试将图像保存到Firebase Storage,但我注意到从图库/Google 云端硬盘保存的几乎所有图片都会旋转。(通常是 90 度)

阅读较早的帖子后,我意识到这是一个常见问题,我尝试通过尝试来自此 post 或来自此 one 、此 one 的解决方案来解决它,我可以继续。

大约 8 小时后,我终于决定在这里提问。 是否有更好、更简单的实现来阻止图像旋转?

我使用ExifInterface(始终返回 0)的最后一个(显然是不成功的)实现是这个:
选择图片

Intent intent = new Intent();

            intent.setType("image/*");
            intent.setAction(Intent.ACTION_PICK);

            startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);

OnActivityResult

    @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {

        Uri selectedImage = data.getData();

        String[] filePathColumn = {MediaStore.Images.Media.DATA};

        Cursor cursor = getContext().getContentResolver().query(selectedImage, filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String filePath = cursor.getString(columnIndex);
        cursor.close();

        int rotateImageAngle = getPhotoOrientation(getContext(), selectedImage, filePath);

        try {
            bitmap = MediaStore.Images.Media.getBitmap(getContext().getContentResolver(), selectedImage);
            // Log.d(TAG, String.valueOf(bitmap));
            addPictureButton.setText("Done");
        } catch (IOException e) {
            e.printStackTrace();
        }

        if( bitmap != null)
        {
            RotateBitmap(bitmap , rotateImageAngle);
        }
    }

辅助方法

 public int getPhotoOrientation(Context context, Uri imageUri, String imagePath){
    int rotate = 0;
    try {
        context.getContentResolver().notifyChange(imageUri, null);
        File imageFile = new File(imagePath);

        ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

        switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_270:
                rotate = 270;
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                rotate = 180;
                break;
            case ExifInterface.ORIENTATION_ROTATE_90:
                rotate = 90;
                break;
        }

        Log.i("RotateImage", "Exif orientation: " + orientation);
        Log.i("RotateImage", "Rotate value: " + rotate);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return rotate;
}
public static Bitmap RotateBitmap(Bitmap source, int angle)
{
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);
    return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}

注意:如果需要,我将添加将图像上传到 Firebase 的代码

【问题讨论】:

    标签: android bitmap android-bitmap image-rotation


    【解决方案1】:

    感谢this post,我找到了解决方案。

    我将在下面发布代码的修改后的工作版本,供遇到此问题的任何人使用。

    从图库中启动图片选择

                        Intent intent = new Intent(Intent.ACTION_PICK,
                            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    intent.setType("image/*");
                    startActivityForResult(intent, PICK_IMAGE_REQUEST);
    

    OnActivityResult 检查所选图片的原始旋转

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK && requestCode == PICK_IMAGE_REQUEST) {
    
            // Get selected gallery image
            Uri selectedPicture = data.getData();
            // Get and resize profile image
            String[] filePathColumn = {MediaStore.Images.Media.DATA};
            // TRY getactvity() as well if not work
            Cursor cursor = getContext().getContentResolver().query(selectedPicture, filePathColumn, null, null, null);
            cursor.moveToFirst();
    
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();
    
            loadedBitmap = BitmapFactory.decodeFile(picturePath);
    
            ExifInterface exif = null;
            try {
                File pictureFile = new File(picturePath);
                exif = new ExifInterface(pictureFile.getAbsolutePath());
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            int orientation = ExifInterface.ORIENTATION_NORMAL;
    
            if (exif != null)
                orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
    
            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    loadedBitmap = rotateBitmap(loadedBitmap, 90);
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    loadedBitmap = rotateBitmap(loadedBitmap, 180);
                    break;
    
                case ExifInterface.ORIENTATION_ROTATE_270:
                    loadedBitmap = rotateBitmap(loadedBitmap, 270);
                    break;
            }
            imageView.setImageBitmap(loadedBitmap);
        }
    }
    

    RotateBitmap 方法

        public static Bitmap rotateBitmap(Bitmap bitmap, int degrees) {
        Matrix matrix = new Matrix();
        matrix.postRotate(degrees);
        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-30
      • 1970-01-01
      • 2017-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多