首页 > 解决方案 > 只有在三星手机图像从图库上传时才会旋转

问题描述

我正在尝试从我的应用程序上传图像,在尝试从相机或图库上传时,在所有其他设备上一切正常,但是当我尝试使用设备 samsung Galaxy J6 从图库->相机文件夹上传图像时,它得到在图像视图中旋转。当尝试从其他文件夹上传时,它工作正常,只有画廊内的相机文件夹出现此问题。我在谷歌上搜索得到了一些使用 exifinterface 的答案,但这也对我没有帮助。这没有在我的图像视图中显示所选图像,它正在执行 catch 块。请帮助我从过去两天卡住。

这是我的代码:-

        if (requestCode == GALLERY) {
        if (data != null) {
            Uri contentURI = data.getData();
            try {

                Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), contentURI);
                Bitmap converetdImage = getResizedBitmap(bitmap, 200);
                PreferenceManager.saveStringForKey(myContext, AppConstant.PREFERENCE_TAG.IMAGE, AppConstant.getBase64Image(converetdImage));
                Toast.makeText(myContext, "Image Saved!", Toast.LENGTH_SHORT).show();

                /*ByteArrayOutputStream baos=new ByteArrayOutputStream();
                byte [] b=baos.toByteArray();
                String temp=Base64.encodeToString(b, Base64.DEFAULT);*/

                ExifInterface exif = new ExifInterface(contentURI.toString());
                int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);

                Matrix matrix = new Matrix();
                if (orientation == 6) {
                    matrix.postRotate(90);
                }
                else if (orientation == 3) {
                    matrix.postRotate(180);
                }
                else if (orientation == 8) {
                    matrix.postRotate(270);
                }
               Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);

               // imgProfile.setImageBitmap(bitmap);
                imgProfile.setImageBitmap(rotatedBitmap);
                is_image_change = true;

            } catch (IOException e) {
                e.printStackTrace();
                Toast.makeText(myContext, "Failed!", Toast.LENGTH_SHORT).show();
            }
        }

    }

标签: javaandroidimageviewandroid-imageviewandroid-gallery

解决方案


推荐阅读