图库应用,如果选择的照片宽度大到高,照片似乎是垂直的

时间:2013-12-05 08:57:05

标签: android

我尝试使用图库应用。 我的应用程序工作2不同的形式。 从画廊或相机选择, 如果选择照片高度大到宽一切都好。 如果选择的照片宽度大到高,照片看起来是垂直的

实施例, 此图片宽度:3264,高度:2448

enter image description here

我的应用程序似乎是这样;

enter image description here

这是我的解码尺码,对不起英文,谢谢。

private class DecodeSize extends AsyncTask<String,Void,Bitmap> {

        private int reqWidth;
        private int reqHeight;
        private ImageView imageView;
        private final WeakReference<ImageView> imageViewReference;

        private DecodeSize(int reqWidth, int reqHeight, ImageView imageView) {
            this.reqWidth = reqWidth;
            this.reqHeight = reqHeight;
            this.imageViewReference = new WeakReference<ImageView>(imageView);
        }


        public int calculateInSampleSize( BitmapFactory.Options options, int reqWidth, int reqHeight) {
            // Raw height and width of image
            final int height = options.outHeight;
            final int width = options.outWidth;

            int inSampleSize = 1;

            if (height > reqHeight || width > reqWidth) {

                final int halfHeight = height / 2;
                final int halfWidth = width / 2;

                // Calculate the largest inSampleSize value that is a power of 2 and keeps both
                // height and width larger than the requested height and width.
                while ((halfHeight / inSampleSize) > reqHeight
                        && (halfWidth / inSampleSize) > reqWidth) {
                    inSampleSize *= 2;
                }
            }

            return inSampleSize;
        }

        @Override
        protected Bitmap doInBackground(String... params) {
            String photo_path = params[0];

            // First decode with inJustDecodeBounds=true to check dimensions
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(photo_path, options);

            // Calculate inSampleSize
            options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

            // Decode bitmap with inSampleSize set
            options.inJustDecodeBounds = false;
            return BitmapFactory.decodeFile(photo_path, options);
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            if(imageViewReference != null && bitmap !=null) {
                final ImageView imageView = imageViewReference.get();
                if (imageView != null) {
                    imageView.setImageBitmap(bitmap);
                }
            }
        }
    }

    private void decodeSizeFunction(String picturePath) {
        decodeSize = new DecodeSize(200,200,foto_image);
        decodeSize.execute(picturePath);
    }

0 个答案:

没有答案