Android 为什么毕加索将图像设置为Imageview时图像会自动旋转

Android 为什么毕加索将图像设置为Imageview时图像会自动旋转,android,Android,我在模式风景上拍摄bt相机,上传到服务器后…它被风景给了 但当我加载到imageview时,会显示垂直,如下图所示: 我正在使用毕加索将图像加载到Imageview。我希望像Imageview上的原始图像一样显示。。。 请给我建议…太多了 public static void makeImageRequest(Context context, ImageView imageView, final String imageUrl, ProgressBar progressBar) {

我在模式风景上拍摄bt相机,上传到服务器后…它被风景给了

但当我加载到imageview时,会显示垂直,如下图所示:

我正在使用毕加索将图像加载到Imageview。我希望像Imageview上的原始图像一样显示。。。 请给我建议…太多了

public static void makeImageRequest(Context context, ImageView imageView, final String imageUrl, ProgressBar progressBar) {
        final int defaultImageResId = R.drawable.ic_member;
        Picasso.with(context)
                .load(imageUrl)
                .error(defaultImageResId)
                .resize(80, 80)
                .into(imageView);
                }
图像视图:

<ImageView
                                android:layout_centerInParent="true"
                                android:padding="@dimen/_8sdp"
                                android:id="@+id/img_photo"
                                android:layout_width="@dimen/_80sdp"
                                android:layout_height="@dimen/_80sdp"
                                android:layout_gravity="center"
                                android:scaleType="fitCenter"
                                android:adjustViewBounds="true" />

试着用这种方法,它会起作用的

public class MainActivity extends Activity {

    private ImageView imgFromCameraOrGallery;
    private Button btnCamera;
    private Button btnGallery;

    private String imgPath;
    final private int PICK_IMAGE = 1;
    final private int CAPTURE_IMAGE = 2;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imgFromCameraOrGallery = (ImageView) findViewById(R.id.imgFromCameraOrGallery);
        btnCamera = (Button) findViewById(R.id.btnCamera);
        btnGallery = (Button) findViewById(R.id.btnGallery);

        btnCamera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, setImageUri());
                startActivityForResult(intent, CAPTURE_IMAGE);
            }
        });

        btnGallery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent, ""), PICK_IMAGE);
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == CAPTURE_IMAGE) {
                setCapturedImage(getImagePath());
            } else if (requestCode == PICK_IMAGE) {
                imgFromCameraOrGallery.setImageBitmap(BitmapFactory.decodeFile(getAbsolutePath(data.getData())));
            }
        }

    }

    private String getRightAngleImage(String photoPath) {

        try {
            ExifInterface ei = new ExifInterface(photoPath);
            int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
            int degree = 0;

            switch (orientation) {
                case ExifInterface.ORIENTATION_NORMAL:
                    degree = 0;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_90:
                    degree = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    degree = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    degree = 270;
                    break;
                case ExifInterface.ORIENTATION_UNDEFINED:
                    degree = 0;
                    break;
                default:
                    degree = 90;
            }

            return rotateImage(degree,photoPath);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return photoPath;
    }

    private String rotateImage(int degree, String imagePath){

        if(degree<=0){
            return imagePath;
        }
        try{
            Bitmap b= BitmapFactory.decodeFile(imagePath);

            Matrix matrix = new Matrix();
            if(b.getWidth()>b.getHeight()){
                matrix.setRotate(degree);
                b = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(),
                        matrix, true);
            }

            FileOutputStream fOut = new FileOutputStream(imagePath);
            String imageName = imagePath.substring(imagePath.lastIndexOf("/") + 1);
            String imageType = imageName.substring(imageName.lastIndexOf(".") + 1);

            FileOutputStream out = new FileOutputStream(imagePath);
            if (imageType.equalsIgnoreCase("png")) {
                b.compress(Bitmap.CompressFormat.PNG, 100, out);
            }else if (imageType.equalsIgnoreCase("jpeg")|| imageType.equalsIgnoreCase("jpg")) {
                b.compress(Bitmap.CompressFormat.JPEG, 100, out);
            }
            fOut.flush();
            fOut.close();

            b.recycle();
        }catch (Exception e){
            e.printStackTrace();
        }
        return imagePath;
    }

    private void setCapturedImage(final String imagePath){
        new AsyncTask<Void,Void,String>(){
            @Override
            protected String doInBackground(Void... params) {
                try {
                    return getRightAngleImage(imagePath);
                }catch (Throwable e){
                    e.printStackTrace();
                }
                return imagePath;
            }

            @Override
            protected void onPostExecute(String imagePath) {
                super.onPostExecute(imagePath);
                imgFromCameraOrGallery.setImageBitmap(decodeFile(imagePath));
            }
        }.execute();
    }

    public Bitmap decodeFile(String path) {
        try {
            // Decode deal_image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(path, o);
            // The new size we want to scale to
            final int REQUIRED_SIZE = 1024;

            // Find the correct scale value. It should be the power of 2.
            int scale = 1;
            while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE)
                scale *= 2;
            // Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            return BitmapFactory.decodeFile(path, o2);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return null;
    }

    public String getAbsolutePath(Uri uri) {
        if(Build.VERSION.SDK_INT >= 19){
            String id = "";
            if(uri.getLastPathSegment().split(":").length > 1)
                id = uri.getLastPathSegment().split(":")[1];
            else if(uri.getLastPathSegment().split(":").length > 0)
                id = uri.getLastPathSegment().split(":")[0];
            if(id.length() > 0){
                final String[] imageColumns = {MediaStore.Images.Media.DATA };
                final String imageOrderBy = null;
                Uri tempUri = getUri();
                Cursor imageCursor = getContentResolver().query(tempUri, imageColumns, MediaStore.Images.Media._ID + "=" + id, null, imageOrderBy);
                if (imageCursor.moveToFirst()) {
                    return imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA));
                }else{
                    return null;
                }
            }else{
                return null;
            }
        }else{
            String[] projection = { MediaStore.MediaColumns.DATA };
            Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
            if (cursor != null) {
                int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
                cursor.moveToFirst();
                return cursor.getString(column_index);
            } else
                return null;
        }

    }

    private Uri getUri() {
        String state = Environment.getExternalStorageState();
        if(!state.equalsIgnoreCase(Environment.MEDIA_MOUNTED))
            return MediaStore.Images.Media.INTERNAL_CONTENT_URI;

        return MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    }

    public Uri setImageUri() {
        Uri imgUri;
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/",getString(R.string.app_name) + Calendar.getInstance().getTimeInMillis() + ".png");
            imgUri = Uri.fromFile(file);
            imgPath = file.getAbsolutePath();
        }else {
            File file = new File(getFilesDir() ,getString(R.string.app_name) + Calendar.getInstance().getTimeInMillis()+ ".png");
            imgUri = Uri.fromFile(file);
            this.imgPath = file.getAbsolutePath();
        }
        return imgUri;
    }

    public String getImagePath() {
        return imgPath;
    }
}
公共类MainActivity扩展活动{
私人图像查看,从摄像机或通道查看;
私人按钮btnCamera;
私用按钮;
私有字符串imgPath;
最终私有int PICK_图像=1;
最终私有整数捕获_图像=2;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgFromCameraOrGallery=(ImageView)findViewById(R.id.imgFromCameraOrGallery);
btnCamera=(按钮)findViewById(R.id.btnCamera);
btnGallery=(按钮)findViewById(R.id.btnGallery);
btnCamera.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
最终意图=新意图(MediaStore.ACTION\u IMAGE\u CAPTURE);
intent.putExtra(MediaStore.EXTRA_输出,setImageUri());
startActivityForResult(意图、捕获图像);
}
});
btnGallery.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
意图=新意图();
intent.setType(“image/*”);
intent.setAction(intent.ACTION\u GET\u CONTENT);
startActivityForResult(Intent.createChooser(Intent,“”),拾取图像);
}
});
}
@凌驾
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
super.onActivityResult(请求代码、结果代码、数据);
if(resultCode==Activity.RESULT\u确定){
if(requestCode==捕获图像){
setCapturedImage(getImagePath());
}else if(requestCode==拾取图像){
imgFromCameraOrGallery.setImageBitmap(BitmapFactory.decodeFile(getAbsolutePath(data.getData()));
}
}
}
私有字符串getRightAngleImage(字符串光路){
试一试{
ExiFinInterface ei=新的ExiFinInterface(光路);
int-orientation=ei.getAttributeInt(ExifInterface.TAG\u-orientation,ExifInterface.orientation\u-NORMAL);
整数度=0;
开关(方向){
案例ExiFinInterface.ORIENTATION_NORMAL:
度=0;
打破
外壳出口接口。方向旋转90:
度=90;
打破
外壳出口接口.方向旋转180:
度=180;
打破
外壳出口接口方向旋转270:
学位=270;
打破
case ExifInterface.ORIENTATION_未定义:
度=0;
打破
违约:
度=90;
}
返回旋转图像(度、光程);
}捕获(例外e){
e、 printStackTrace();
}
返回光路;
}
私有字符串旋转图像(整数度,字符串图像路径){
如果(degreeb.getHeight()){
矩阵旋转(度);
b=Bitmap.createBitmap(b,0,0,b.getWidth(),b.getHeight(),
矩阵,真);
}
FileOutputStream fOut=新的FileOutputStream(imagePath);
字符串imageName=imagePath.substring(imagePath.lastIndexOf(“/”)+1);
字符串imageType=imageName.substring(imageName.lastIndexOf(“.”+1);
FileOutputStream out=新的FileOutputStream(imagePath);
if(imageType.equalsIgnoreCase(“png”)){
b、 压缩(Bitmap.CompressFormat.PNG,100,out);
}else if(imageType.equalsIgnoreCase(“jpeg”)| | imageType.equalsIgnoreCase(“jpg”)){
b、 压缩(位图.CompressFormat.JPEG,100,输出);
}
fOut.flush();
fOut.close();
b、 回收();
}捕获(例外e){
e、 printStackTrace();
}
返回图像路径;
}
私有void setCapturedImage(最终字符串图像路径){
新建异步任务(){
@凌驾
受保护字符串doInBackground(无效…参数){
试一试{
返回getRightAngleImage(imagePath);
}捕获(可丢弃的e){
e、 printStackTrace();
}
返回图像路径;
}
@凌驾
受保护的void onPostExecute(字符串imagePath){
super.onPostExecute(imagePath);
imgFromCameraOrGallery.setImageBitmap(解码文件(imagePath));
}
}.execute();
}
公共位图解码文件(字符串路径){
试一试{
//解码图像大小
BitmapFactory.Options o=新的BitmapFactory.Options();
o、 inJustDecodeBounds=true;
解码文件(路径,o);
//我们要扩展到的新尺寸
所需的最终int_SIZE=1024;
//找到正确的刻度值。它应该是2的幂。
int标度=1;
而(o.outWidth/scale/2>=所需尺寸和&o.outHeight/scale/2>=所需尺寸)
比例*=2;
//用inSampleSize解码
BitmapFactory.Options o2=新的BitmapFactory.Options();
o2.inSampleSize=刻度;
返回BitmapFactory.decodeFile(路径,o2);
}捕获(可丢弃的e){
e、 printStackTrace();
}
public class ImageRotationDetectionHelper {

    public static int getCameraPhotoOrientation(String imageFilePath) {
        int rotate = 0;
        try {

            ExifInterface exif;

            exif = new ExifInterface(imageFilePath);
            String exifOrientation = exif
                    .getAttribute(ExifInterface.TAG_ORIENTATION);
            Log.d("exifOrientation", exifOrientation);
            int orientation = exif.getAttributeInt(
                    ExifInterface.TAG_ORIENTATION,
                    ExifInterface.ORIENTATION_NORMAL);
            Log.d(ImageRotationDetectionHelper.class.getSimpleName(), "orientation :" + orientation);
            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 (IOException e) {
            e.printStackTrace();
        }

        return rotate;
    }
}
public class MainActivity extends Activity {

    private ImageView imgFromCameraOrGallery;
    private Button btnCamera;
    private Button btnGallery;

    private String imgPath;
    final private int PICK_IMAGE = 1;
    final private int CAPTURE_IMAGE = 2;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imgFromCameraOrGallery = (ImageView) findViewById(R.id.imgFromCameraOrGallery);
        btnCamera = (Button) findViewById(R.id.btnCamera);
        btnGallery = (Button) findViewById(R.id.btnGallery);

        btnCamera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, setImageUri());
                startActivityForResult(intent, CAPTURE_IMAGE);
            }
        });

        btnGallery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent, ""), PICK_IMAGE);
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == CAPTURE_IMAGE) {
                setCapturedImage(getImagePath());
            } else if (requestCode == PICK_IMAGE) {
                imgFromCameraOrGallery.setImageBitmap(BitmapFactory.decodeFile(getAbsolutePath(data.getData())));
            }
        }

    }

    private String getRightAngleImage(String photoPath) {

        try {
            ExifInterface ei = new ExifInterface(photoPath);
            int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
            int degree = 0;

            switch (orientation) {
                case ExifInterface.ORIENTATION_NORMAL:
                    degree = 0;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_90:
                    degree = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    degree = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    degree = 270;
                    break;
                case ExifInterface.ORIENTATION_UNDEFINED:
                    degree = 0;
                    break;
                default:
                    degree = 90;
            }

            return rotateImage(degree,photoPath);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return photoPath;
    }

    private String rotateImage(int degree, String imagePath){

        if(degree<=0){
            return imagePath;
        }
        try{
            Bitmap b= BitmapFactory.decodeFile(imagePath);

            Matrix matrix = new Matrix();
            if(b.getWidth()>b.getHeight()){
                matrix.setRotate(degree);
                b = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(),
                        matrix, true);
            }

            FileOutputStream fOut = new FileOutputStream(imagePath);
            String imageName = imagePath.substring(imagePath.lastIndexOf("/") + 1);
            String imageType = imageName.substring(imageName.lastIndexOf(".") + 1);

            FileOutputStream out = new FileOutputStream(imagePath);
            if (imageType.equalsIgnoreCase("png")) {
                b.compress(Bitmap.CompressFormat.PNG, 100, out);
            }else if (imageType.equalsIgnoreCase("jpeg")|| imageType.equalsIgnoreCase("jpg")) {
                b.compress(Bitmap.CompressFormat.JPEG, 100, out);
            }
            fOut.flush();
            fOut.close();

            b.recycle();
        }catch (Exception e){
            e.printStackTrace();
        }
        return imagePath;
    }

    private void setCapturedImage(final String imagePath){
        new AsyncTask<Void,Void,String>(){
            @Override
            protected String doInBackground(Void... params) {
                try {
                    return getRightAngleImage(imagePath);
                }catch (Throwable e){
                    e.printStackTrace();
                }
                return imagePath;
            }

            @Override
            protected void onPostExecute(String imagePath) {
                super.onPostExecute(imagePath);
                imgFromCameraOrGallery.setImageBitmap(decodeFile(imagePath));
            }
        }.execute();
    }

    public Bitmap decodeFile(String path) {
        try {
            // Decode deal_image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(path, o);
            // The new size we want to scale to
            final int REQUIRED_SIZE = 1024;

            // Find the correct scale value. It should be the power of 2.
            int scale = 1;
            while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE)
                scale *= 2;
            // Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            return BitmapFactory.decodeFile(path, o2);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return null;
    }

    public String getAbsolutePath(Uri uri) {
        if(Build.VERSION.SDK_INT >= 19){
            String id = "";
            if(uri.getLastPathSegment().split(":").length > 1)
                id = uri.getLastPathSegment().split(":")[1];
            else if(uri.getLastPathSegment().split(":").length > 0)
                id = uri.getLastPathSegment().split(":")[0];
            if(id.length() > 0){
                final String[] imageColumns = {MediaStore.Images.Media.DATA };
                final String imageOrderBy = null;
                Uri tempUri = getUri();
                Cursor imageCursor = getContentResolver().query(tempUri, imageColumns, MediaStore.Images.Media._ID + "=" + id, null, imageOrderBy);
                if (imageCursor.moveToFirst()) {
                    return imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA));
                }else{
                    return null;
                }
            }else{
                return null;
            }
        }else{
            String[] projection = { MediaStore.MediaColumns.DATA };
            Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
            if (cursor != null) {
                int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
                cursor.moveToFirst();
                return cursor.getString(column_index);
            } else
                return null;
        }

    }

    private Uri getUri() {
        String state = Environment.getExternalStorageState();
        if(!state.equalsIgnoreCase(Environment.MEDIA_MOUNTED))
            return MediaStore.Images.Media.INTERNAL_CONTENT_URI;

        return MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    }

    public Uri setImageUri() {
        Uri imgUri;
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/",getString(R.string.app_name) + Calendar.getInstance().getTimeInMillis() + ".png");
            imgUri = Uri.fromFile(file);
            imgPath = file.getAbsolutePath();
        }else {
            File file = new File(getFilesDir() ,getString(R.string.app_name) + Calendar.getInstance().getTimeInMillis()+ ".png");
            imgUri = Uri.fromFile(file);
            this.imgPath = file.getAbsolutePath();
        }
        return imgUri;
    }

    public String getImagePath() {
        return imgPath;
    }
}
Resolution : 3264 x 2448
Orientation : rotate 90
Picasso.with(MainActivity.this)
    .load(imageURL) // web image url
    .fit().centerInside()
    .transform(transformation)
    .rotate(90)                    //if you want to rotate by 90 degrees
    .error(R.drawable.ic_launcher)
    .placeholder(R.drawable.ic_launcher)
    .into(imageview)
    });
    dependencies {
  // Your app's other dependencies
  compile 'com.github.bumptech.glide:glide.3.7.0'
}
Glide.with(this).load("image_url").into(imageView);
String imagePath = /* your image path here */
Picasso.get()
       .load(Uri.parseString(getRightAngleImage(imagePath)))
       .into(/* your ImageView id here */);
private String getRightAngleImage(String photoPath) {

    try {
        ExifInterface ei = new ExifInterface(photoPath);
        int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
        int degree = 0;

        switch (orientation) {
            case ExifInterface.ORIENTATION_NORMAL:
                degree = 0;
                break;
            case ExifInterface.ORIENTATION_ROTATE_90:
                degree = 90;
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                degree = 180;
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                degree = 270;
                break;
            case ExifInterface.ORIENTATION_UNDEFINED:
                degree = 0;
                break;
            default:
                degree = 90;
        }

        return rotateImage(degree,photoPath);

    } catch (Exception e) {
        e.printStackTrace();
    }

    return photoPath;
}

private String rotateImage(int degree, String imagePath){

    if(degree<=0){
        return imagePath;
    }
    try{
        Bitmap b= BitmapFactory.decodeFile(imagePath);

        Matrix matrix = new Matrix();
        if(b.getWidth()>b.getHeight()){
            matrix.setRotate(degree);
            b = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(),
                    matrix, true);
        }

        FileOutputStream fOut = new FileOutputStream(imagePath);
        String imageName = imagePath.substring(imagePath.lastIndexOf("/") + 1);
        String imageType = imageName.substring(imageName.lastIndexOf(".") + 1);

        FileOutputStream out = new FileOutputStream(imagePath);
        if (imageType.equalsIgnoreCase("png")) {
            b.compress(Bitmap.CompressFormat.PNG, 100, out);
        }else if (imageType.equalsIgnoreCase("jpeg")|| imageType.equalsIgnoreCase("jpg")) {
            b.compress(Bitmap.CompressFormat.JPEG, 100, out);
        }
        fOut.flush();
        fOut.close();

        b.recycle();
    }catch (Exception e){
        e.printStackTrace();
    }
    return imagePath;
}
    fun getRotation(context: Context, selectedImage: Uri): Float {
        val ei = ExifInterface(context.contentResolver.openInputStream(selectedImage)!!)
        val orientation: Int =
            ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL)
        return when (orientation) {
            ExifInterface.ORIENTATION_NORMAL -> 0f
            ExifInterface.ORIENTATION_ROTATE_90 -> 90f
            ExifInterface.ORIENTATION_ROTATE_180 -> 180f
            ExifInterface.ORIENTATION_ROTATE_270 -> 270f
            ExifInterface.ORIENTATION_UNDEFINED -> 0f
            else -> 90f
        }
    }
            val inputStream = context.contentResolver.openInputStream(IMAGE_URI_HERE)
            val file = File(filesDir, "image.YOUR_EXT")
            if (inputStream != null) {
                val outputStream: OutputStream = FileOutputStream(file)
                val BUFFER_SIZE = 1024 * 2
                val buffer = ByteArray(BUFFER_SIZE)
                var n: Int
                try {
                    BufferedInputStream(inputStream, BUFFER_SIZE).use { `in` ->
                        BufferedOutputStream(outputStream, BUFFER_SIZE).use { out ->
                            while (`in`.read(buffer, 0, BUFFER_SIZE).also { n = it } != -1) {
                                out.write(buffer, 0, n)
                            }
                            out.flush()
                        }
                    }
                } catch (e: IOException) {
                    e.printStackTrace()
                    myFile.isSuccess = false
                    myFile.errorMsg = "05" //IOException 2
                    return myFile
                }
                inputStream.close()
                outputStream.close()
            } 
val bitmapOriginal = BitmapFactory.decodeFile(file.filePath)
val rotatedBitmap = rotateBitmap(bitmapOriginal, getRotation(context, uri)) //This is your final Bitmap
fun rotateImage(img: Bitmap, degree: Float): Bitmap {
        val matrix = Matrix()
        matrix.postRotate(degree)
        val rotatedImg = Bitmap.createBitmap(img, 0, 0, img.width, img.height, matrix, true)
        img.recycle()
        return rotatedImg
    }
file.delete()