(オリジナル)私が書いたいくつかのツールを共有する(7)ビットマップと文字列の変換

言うことはあまりありませんが、コードに移動するだけです

ビットマップToString
    public static String bitmapToString(Bitmap bitmap) {
        //将Bitmap转换成字符串
        String string = null;
        ByteArrayOutputStream bStream = new ByteArrayOutputStream();
        //参数1为转换的图片格式,参数二为压缩的百分比,这里是10%
        bitmap.compress(Bitmap.CompressFormat.JPEG, 10, bStream);
        byte[] bytes = bStream.toByteArray();
        string = Base64.encodeToString(bytes, Base64.DEFAULT);
        return string;
    }

 

stringToBitmap
    public Bitmap stringToBitmap(String string) {
        // 将String转换成Bitmap类型
        Bitmap bitmap = null;
        try {
            byte[] bitmapArray;
            bitmapArray = Base64.decode(string, Base64.DEFAULT);
            bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0,
                    bitmapArray.length);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bitmap;
    }

 

おすすめ

転載: blog.csdn.net/Android_xiong_st/article/details/89212371