当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Glide Android encoding

问题描述:

I get confused how to encode bitmap with Glide. In past way, I have private Bitmap bitmap; that initialize Bitmap globally, and set

 @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();

}

}

}

But this way consume too much memory. Then I change to,

@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();

}

}

Everything looks good, it became faster when load image. When I have to encode my bitmap with,

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;

}

it shows fatal exception in java, and getting error converting bitmap with getBitmapToString() method. Any solutions?

网友答案:
  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);
                    }
                });
分享给朋友:
您可能感兴趣的文章:
随机阅读: