Android将图片转换为字节流,进一步转换为String类型的数据

通过编写一个子函数实现该功能,具体代码如下:

/**
     * 将图片转换成字节流,并进一步转换成String类型的数据
     */
    public static String putImageToShare(Context mContext, ImageView imageView) {
    
    
        BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
        Bitmap bitmap = drawable.getBitmap();
        ByteArrayOutputStream byStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 80, byStream);
        byte[] byteArray = byStream.toByteArray();
        String imgString = new String(Base64.encodeToString(byteArray, Base64.DEFAULT));
        return imgString;
    }

——————————————————————————
最后贴一下我的个人公众号:微信搜索“茶迁”或扫描下图。平时会更新一些编程相关的文章,欢迎大家关注~
茶迁

猜你喜欢

转载自blog.csdn.net/weixin_46269688/article/details/111307848
今日推荐