文件各格式转化成Base64字符串b

时间:2021-04-27 21:48:55

1.编码:此处以音频为例,转化成base64字符串

在mainActivity中

注意:

//mnt/sdcard/xxx/xxx.MP3 手机路径格式

String fileBase64=encodeBase64File("/mnt/sdcard/documents/tttt.3gp");

public static String encodeBase64File(String path) throws Exception {
file = new File(path);
FileInputStream inputFile = new FileInputStream(file);
byte[] buffer = new byte[(int)file.length()];

inputFile.read(buffer);
inputFile.close();
return Base64.encodeToString(buffer, Base64.DEFAULT);
}
2. 解码

/**
* decoderBase64File:(将base64字符解码保存文件). <br/>
* @author guhaizhou@126.com
* @param base64Code 编码后的字串
* @param savePath  文件保存路径        注意:要保存的格式  xxxx.MP3
* @throws Exception
* @since JDK 1.6
*/
public static void decoderBase64File(String base64Code,String savePath) throws Exception {
//byte[] buffer = new BASE64Decoder().decodeBuffer(base64Code);
byte[] buffer =Base64.decode(base64Code, Base64.DEFAULT);
FileOutputStream out = new FileOutputStream(savePath);
out.write(buffer);
out.close();

}

注解参考:http://blog.csdn.net/jbboy/article/details/22919821