首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Glide Android编码

Glide Android编码
EN

Stack Overflow用户
提问于 2016-02-12 12:14:32
回答 1查看 1.6K关注 0票数 0

我搞不懂如何用Glide编码位图。在过去,我使用private Bitmap bitmap;全局初始化位图,并设置

代码语言:javascript
复制
    @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();
                }

            }
        }

但是这种方式消耗了太多的内存。然后我改成,

代码语言:javascript
复制
@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();
        }
    }

一切看起来都很好,当加载图像时变得更快。当我必须用来编码我的位图时,

代码语言:javascript
复制
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()方法转换位图时出错。有什么解决方案吗?

EN

回答 1

Stack Overflow用户

发布于 2016-02-12 12:44:12

代码语言:javascript
复制
  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);
                    }
                });
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35354722

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档