0

我正在编写一个 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();
                }

        }
    }
}
4

1 回答 1

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());
}
于 2013-02-24T19:22:11.610 回答