图片跟base64字节互转

    public static String getFileBase64(String url) {
        try {
            File file = new File(url);
            FileInputStream inputFile = new FileInputStream(file);
            byte[] buffer = new byte[(int) file.length()];
            inputFile.read(buffer);
            inputFile.close();
            return Base64.encodeToString(buffer, Base64.DEFAULT);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "";
    }

    public Bitmap getBitmapForBase64Str(String base64Str) {
        if (base64Str.contains(",")){
            base64Str = base64Str.split(",")[1];
        }
        byte[] decode = Base64.decode(base64Str, Base64.DEFAULT);
        return BitmapFactory.decodeByteArray(decode, 0, decode.length);
    }

 

posted @ 2020-08-25 16:29  码弄  阅读(444)  评论(0编辑  收藏  举报