Android 常用API使用 之 Base64

来源:互联网 发布:武林外传展堂 知乎 编辑:程序博客网 时间:2024/05/16 13:59

Base64编码文件为字符串

public static String encodeBase64File(File file) throws Exception {    FileInputStream inputFile = new FileInputStream(file);    byte[] buffer = new byte[(int) file.length()];    inputFile.read(buffer);    inputFile.close();    return Base64.encodeToString(buffer, Base64.DEFAULT);}

Base64编码Bitmap为字符串

public String bitmapToString(Bitmap bitmap) {    ByteArrayOutputStream bitmapStream = new ByteArrayOutputStream();    bitmap.compress(Bitmap.CompressFormat.PNG, 100, bitmapStream);    byte[] bytes = bitmapStream.toByteArray();    return Base64.encodeToString(bytes, Base64.DEFAULT);}
0 0
原创粉丝点击