Android图片转换(base64→BitMap)及图片保存

1.将图片转成base64

private String BitMapToBase64(String imgPath) {      
                Bitmap bitmap = BitmapFactory.decodeFile(imgPath);
                ByteArrayOutputStream os = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
                byte[] bytes = os.toByteArray();
                byte[] encode = Base64.encode(bytes,Base64.DEFAULT);
                String encodeStr = new String(encode);
                return encodeStr;
}

2.将base64的图片编码转换成BitMap

public Bitmap Base64ToBitMap(String base64) {
        byte[] decode = Base64.decode(base64, Base64.DEFAULT);
        Bitmap bitmap = BitmapFactory.decodeByteArray(decode, 0, decode.length);
        return bitmap;
    }

3.图片保存

// string & str:图片名称
private void saveBitmap(Bitmap bitmap,String string) {
        try {
            String path = Environment.getExternalStorageDirectory().getPath()
                    +"/"+str+"_"+string+".jpg";
            OutputStream stream = new FileOutputStream(path);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 90, stream);
            stream.close();
            
            //MyToast.getToast(this,"图片保存成功!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

你可能感兴趣的:(Android图片转换(base64→BitMap)及图片保存)