繁体   English   中英

Glide Android编码

[英]Glide Android encoding

我很困惑如何使用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()方法转换位图时出错。 有什么办法吗?

  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);
                    }
                });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM