图像旋转因图像而异

时间:2014-05-29 06:04:26

标签: android image image-rotation

我制作了一个应用程序,用户从图库中选择一个图像,该图像显示在imageview中。我使用了调整大小的方法来避免内存不足问题。问题是当我选择宽度较低的图像时和图像在图像视图中正确设置的高度,但是当图像宽度和高度很大时,图像会在图像视图中旋转并设置。为什么会发生这种情况。请帮助

CaptureImage

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult (requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == RESULT_OK && null != data) {


            selectedImage = data.getData ();
            try {
                resizedGallery = decodeUri (getApplicationContext (), selectedImage);
            } catch (FileNotFoundException e) {
                e.printStackTrace ();
            }

            dialog = new Dialog (CaptureImage.this, android.R.style.Theme_DeviceDefault);
            dialog.requestWindowFeature (Window.FEATURE_NO_TITLE);
            dialog.setContentView (R.layout.confirmation_image);
            dialog.getWindow ().setLayout (ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            ImageView image = (ImageView) dialog.findViewById (R.id.iv_selected_image);
            image.setImageBitmap (resizedGallery);
            image.setRotation (90);
            ok_gallery = (Button) dialog.findViewById (R.id.bt_ok_gallery);
            cancel = (Button) dialog.findViewById (R.id.bt_cancel);
            ok_gallery.setOnClickListener (this);
            cancel.setOnClickListener (this);
            dialog.show (); 

decodeUri方法

public static Bitmap decodeUri(Context c, Uri uri)
            throws FileNotFoundException {
        BitmapFactory.Options o = new BitmapFactory.Options ();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream (c.getContentResolver ().openInputStream (uri), null, o);

        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;

        while (true) {
            if (width_tmp / 2 < 400 || height_tmp / 2 < 200)
                break;
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }

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

1 个答案:

答案 0 :(得分:0)

在将bitmap设置为imageview之后,因为调用image.setRotation(90)而发生了这种情况; 删除它,然后再试一次。