我搞不懂如何用Glide编码位图。在过去,我使用private Bitmap bitmap;全局初始化位图,并设置
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK) {
Uri filePath = data.getData();
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
imageView.setImageBitmap(bitmap);
} catch (Exception e) {
e.printStackTrace();
}
}
}但是这种方式消耗了太多的内存。然后我改成,
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK) {
Uri filePath = data.getData();
// load Glide to imageView
Glide.with(this)
.load(filePath)
.into(imageView);
// set bitmap variable
bitmap = Glide.with(this)
.load(filePath)
.asBitmap()
.into(100,100).get();
}
}一切看起来都很好,当加载图像时变得更快。当我必须用来编码我的位图时,
public String getBitmapToString(Bitmap bitmap) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
byte[] imageBytes = byteArrayOutputStream.toByteArray();
String encoded = Base64.encodeToString(imageBytes, Base64.DEFAULT);
return encoded;
}在java中显示致命异常,并使用getBitmapToString()方法转换位图时出错。有什么解决方案吗?
发布于 2016-02-12 12:44:12
Glide.with(this)
.load(filePath)
.asBitmap()
.override(size, size) //if you want particular size
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {
imageView.setImage(ImageSource.bitmap(bitmap));
thumbView.setImageBitmap(bitmap);
}
});https://stackoverflow.com/questions/35354722
复制相似问题