简体   繁体   中英

Image rotated when chosen from gallery

I am using the following code to get the orientation of the selected image so that if it was taken vertically, when selected from gallery it would not be shown horizontally.

The error is occuring in the File imageFile = new File(selectedImage.toString()); in the parameter selectedImage.toString()) since when I changed the initial int rotate=90 and chose a vertical image the result was good.

Am I passing the parameter to the File correct?

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    BitmapFactory.Options options;
    if (resultCode == RESULT_OK && requestCode == PICTURE_SELECTED) {
        try {
            options = new BitmapFactory.Options();
            options.inSampleSize = 4;
            Uri selectedImage = data.getData();
            InputStream stream = getContentResolver().openInputStream(selectedImage);
            Bitmap yourSelectedImage = BitmapFactory.decodeStream(stream, null, options);
            stream.close();
            //orientation
            try {
                int rotate = 0;
                try {
                    File imageFile = new File(selectedImage.toString());
                    ExifInterface exif = new ExifInterface(
                            imageFile.getAbsolutePath());
                    int orientation = exif.getAttributeInt(
                            ExifInterface.TAG_ORIENTATION,
                            ExifInterface.ORIENTATION_NORMAL);

                    switch (orientation) {
                        case ExifInterface.ORIENTATION_ROTATE_270:
                            rotate = 270;
                            break;
                        case ExifInterface.ORIENTATION_ROTATE_180:
                            rotate = 180;
                            break;
                        case ExifInterface.ORIENTATION_ROTATE_90:
                            rotate = 90;
                            break;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                Matrix matrix = new Matrix();
                matrix.postRotate(rotate);
                yourSelectedImage = Bitmap.createBitmap(yourSelectedImage , 0, 0, yourSelectedImage.getWidth(), yourSelectedImage.getHeight(), matrix, true);  }
            catch (Exception e) {}
            //end of orientation

            imgButton.setScaleType(ImageView.ScaleType.FIT_CENTER);
            imgButton.setImageBitmap(yourSelectedImage);
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), "Could not open file.", Toast.LENGTH_LONG).show();
        }
    } else {
        Toast.makeText(getApplicationContext(), "Image was not selected", Toast.LENGTH_LONG).show();
    }
    super.onActivityResult(requestCode, resultCode, data);
}

Modify your method like this:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    BitmapFactory.Options options;
    if (resultCode == RESULT_OK && requestCode == PICTURE_SELECTED) {
        try {
            options = new BitmapFactory.Options();
            options.inSampleSize = 4;
            Uri selectedImage = data.getData();

            String[] filePath = {MediaStore.Images.Media.DATA};

            Cursor cursor = getContentResolver().query(selectedImage, filePath, null, null, null);
                    cursor.moveToFirst();
            String mImagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));

            InputStream stream = getContentResolver().openInputStream(selectedImage);
            Bitmap yourSelectedImage = BitmapFactory.decodeStream(stream, null, options);
            stream.close();
            //orientation
            try {
                int rotate = 0;
                try {
                    ExifInterface exif = new ExifInterface(
                            mImagePath);
                    int orientation = exif.getAttributeInt(
                            ExifInterface.TAG_ORIENTATION,
                            ExifInterface.ORIENTATION_NORMAL);

                    switch (orientation) {
                        case ExifInterface.ORIENTATION_ROTATE_270:
                            rotate = 270;
                            break;
                        case ExifInterface.ORIENTATION_ROTATE_180:
                            rotate = 180;
                            break;
                        case ExifInterface.ORIENTATION_ROTATE_90:
                            rotate = 90;
                            break;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                Matrix matrix = new Matrix();
                matrix.postRotate(rotate);
                yourSelectedImage = Bitmap.createBitmap(yourSelectedImage , 0, 0, yourSelectedImage.getWidth(), yourSelectedImage.getHeight(), matrix, true);  }
            catch (Exception e) {}
            //end of orientation

            imgButton.setScaleType(ImageView.ScaleType.FIT_CENTER);
            imgButton.setImageBitmap(yourSelectedImage);
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), "Could not open file.", Toast.LENGTH_LONG).show();
        }
    } else {
        Toast.makeText(getApplicationContext(), "Image was not selected", Toast.LENGTH_LONG).show();
    }
}

对于图像处理,编译picasso是使用它的最佳库,您可以轻松使用它处理图像,还可以从缓存中加载图像并提高应用程序的性能,因此请避免执行大量代码,并在构建gradle中添加以下行,然后使用picasso在您的代码中。

compile 'com.squareup.picasso:picasso:2.5.0'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM