从库中加载图片会旋转

时间:2012-11-14 17:41:35

标签: android android-imageview

我正在编写一个Android应用程序,我正在为用户提供从库中加载图片的功能。

图片已加载但旋转了90度。有时它旋转-90度。 当你通过你的图像查看它时,如何以“相同的方式”加载图片? 因此,如果它被旋转,那么确保加载它旋转它。但如果它是正确的,那么它应该以相同的方式加载

非常感谢

很抱歉,我应该添加我的代码,这是:

        try {
            Intent choosePictureIntent = new Intent(Intent.ACTION_PICK, Images.Media.INTERNAL_CONTENT_URI);
            startActivityForResult(choosePictureIntent, REQUEST_CHOOSE_IMAGE);
        } catch (Exception e) {
            e.printStackTrace();
        }
        break;
@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

        switch(requestCode) { 
        case REQUEST_CHOOSE_IMAGE:
            if(resultCode == RESULT_OK){  
                Uri selectedImage = imageReturnedIntent.getData();
                InputStream imageStream;
                try {
                    imageStream = getContentResolver().openInputStream(selectedImage);
                    BitmapFactory.Options options=new BitmapFactory.Options();
                    options.inSampleSize = 8;
                    Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream, null, options );
                    ivPictureChosen.setImageBitmap(yourSelectedImage); //ivPictureChosen is image view to display the picture

                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    Toast.makeText(this, "Couldn't pick image", Toast.LENGTH_SHORT).show();
                }

        }
    }
}

1 个答案:

答案 0 :(得分:0)

尝试以下代码。它检查图像的方向,然后相应地旋转它。我遇到的问题是内存不足,还没有找到超出VM buget的解决方案。这应该适用于较小的图像以及当您的应用尚未使用大量内存时。

ExifInterface ei = new ExifInterface(filePath);
                int orientation = ei.getAttributeInt(
                        ExifInterface.TAG_ORIENTATION,
                        ExifInterface.ORIENTATION_NORMAL);

                switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    rotateBitmap(bitmap, 90);
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    rotateBitmap(bitmap, 180);
                    break;

                }

public static void rotateBitmap(final Bitmap source, int mRotation){
    int targetWidth=source.getWidth();
    int targetHeight=source.getHeight();
    Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight, source.getConfig());
    Canvas canvas = new Canvas(targetBitmap);
    Matrix matrix = new Matrix();
    matrix.setRotate(mRotation,source.getWidth()/2,source.getHeight()/2);
    canvas.drawBitmap(source, matrix, new Paint());
}