从图库中旋转图像

时间:2015-06-18 08:26:11

标签: android bitmap imageview autorotate

我想从图库中加载图片,一切都很好,但图片显示不正确,有些东西会旋转图像。

我使用课程ExifUtil

    Uri selectedImage = imageReturnedIntent.getData();

                            try {

                                String imagePath = selectedImage.toString();
                                Bitmap myBitmap = decodeUri(selectedImage);

                                Bitmap orientation = ExifUtil.rotateBitmap(imagePath, myBitmap);
                                photoEdit.setImageBitmap(orientation); 

                            } catch (Exception e) {
                                e.printStackTrace();
                            }

 //code method decodeUri

private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException {
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(
                getContentResolver().openInputStream(selectedImage), null, o);

        final int REQUIRED_SIZE = 100;

        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;
        while (true) {
            if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) {
                break;
            }
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }

        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeStream(
                getContentResolver().openInputStream(selectedImage), null, o2);
    }

0 个答案:

没有答案