Java 如何保存位图的正确旋转

Java 如何保存位图的正确旋转,java,android,bitmap,kotlin,android-glide,Java,Android,Bitmap,Kotlin,Android Glide,我有一个问题:当我加载从相机或画廊中获取的照片时,使用以下代码: if (resultCode == Activity.RESULT_OK) { if (requestCode == REQUEST_TAKE_PHOTO || requestCode == REQUEST_PICK_PHOTO) //dalla fotocamera { if (data != null)mMediaUri = data.getData();

我有一个问题:当我加载从相机或画廊中获取的照片时,使用以下代码:

if (resultCode == Activity.RESULT_OK) {
        if (requestCode == REQUEST_TAKE_PHOTO || requestCode == REQUEST_PICK_PHOTO)
        //dalla fotocamera
        {
            if (data != null)mMediaUri = data.getData();

            Glide.with(this).load(mMediaUri).bitmapTransform(new CenterCrop(context), new RoundCornerTransformation(context, 15, 2)).into(photo);
        }
}
  if(user.getPicUrl() != null) {
        Glide.with(this).load(user.getPicture()).bitmapTransform(new CenterCrop(context), new RoundCornerTransformation(context, 15, 2)).into(photo);
        PICTURE_INSERTED = true;
    }
图片在imageView中以正确的方式显示。 否则,我需要压缩位图,因此我编写了以下代码来进行压缩:

 fun compressBitmapInBackgroundtoByteArray(uri: Uri?,context: Context, callback: OnFinishedCallback) {

    Observable.create<ByteArray> { s ->


        var imageStream: InputStream? = null
        try {
            imageStream = context.getContentResolver().openInputStream(uri)
        } catch (e: FileNotFoundException) {
            s.onError(e)
        }

        val bmp = BitmapFactory.decodeStream(imageStream)

        var stream: ByteArrayOutputStream? = ByteArrayOutputStream()
        bmp.compress(Bitmap.CompressFormat.JPEG, 0, stream)
        bmp.recycle()
        val compressed:ByteArray? =(stream?.toByteArray())

        try {
            stream?.close()

        } catch (e: IOException) {
            s.onError(e)
        }

        s.onNext(compressed!!)
        s.onComplete()

    }.subscribeOn(Schedulers.computation())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeBy (
                    onNext = { compress -> callback.onNext(compress) },
                    onError =  { error-> callback.onError(error.message) },
                    onComplete = { callback.onCompleted() }
            )


}
这幅画旋转了90度。
我读过Glide支持Exif旋转,但在本例中它不起作用。为什么?有没有一种方法可以“存储”正确的方向以保存在服务器中?谢谢

您必须将EXIF数据从原始图像复制到最终调整大小的图像。您可以使用ExifInterface进行此操作

比如,

val oldExif = ExifInterface(oldImagePath)
val exifOrientation = oldExif.getAttribute(ExifInterface.TAG_ORIENTATION)

if (exifOrientation != null) {
 val newExif = ExifInterface(imagePath)
 newExif.setAttribute(ExifInterface.TAG_ORIENTATION, exifOrientation)
 newExif.saveAttributes()
}

也许这个库对你有帮助。它是min sdk 14。我现在正在看ExiFinInterface。它甚至支持API 5。可能你的导入是错误的。我的导入是这样的:import android.media.ExiFinInterface