【发布时间】:2017-07-08 12:40:14
【问题描述】:
我遇到了这个问题:当我使用以下代码加载从相机或画廊获取的照片时:
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);
}
}
图片在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() }
)
}
这会失去正确的方向。事实上,当我尝试使用以下代码加载我给出的 byteArray 时:
if(user.getPicUrl() != null) {
Glide.with(this).load(user.getPicture()).bitmapTransform(new CenterCrop(context), new RoundCornerTransformation(context, 15, 2)).into(photo);
PICTURE_INSERTED = true;
}
图片旋转了 90 度。 我读过 Glide 支持 Exif 旋转,但在这种情况下它不起作用。为什么?有一种方法可以“存储”正确的方向以保存在服务器中吗?谢谢
【问题讨论】:
标签: java android bitmap kotlin android-glide