问题0004 - 获取到的相册图片方向有误

118 阅读1分钟

相关文章

实际代码

String imagePath = 本地照片实际路径;
Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
Matrix matrix = gainCorrectMatrix(imagePath);
if(matrix != null){
    bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}

private Matrix gainCorrectMatrix(String imagePath) {
    Matrix matrix = new Matrix();
    ExifInterface exifInterface = null;
    try {
        exifInterface = new ExifInterface(imagePath);
        int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
        switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                matrix.postRotate(90f);
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                matrix.postRotate(180f);
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                matrix.postRotate(270f);
                break;
            default:
                matrix = null;
                break;
        }
        return matrix;
    } catch (Exception e) {
        Log.d(TAG,"gainCorrectMatrix err:"+ e.getMessage());
        e.printStackTrace();
        return null;
    }
}