Base64 转 File
public File base64ToFile(String base64, String filePath) { File file = new File(filePath); byte[] buffer; try { BASE64Decoder base64Decoder = new BASE64Decoder(); buffer = base64Decoder.decodeBuffer(base64); FileOutputStream out = new FileOutputStream(filePath); out.write(buffer); out.close(); } catch (Exception e) { Log.e("TAG", "异常信息:" + e.getMessage()); } return file; }
File 转 Base64
public String fileToBase64(String filePath) {File file = new File(filePath);FileInputStream inputFile;try {inputFile = new FileInputStream(file);byte[] buffer = new byte[inputFile.available()];inputFile.read(buffer);inputFile.close();BASE64Encoder base64Encoder = new BASE64Encoder();Log.i("encodeFileToBase64", "encode = " + base64Encoder.encode(buffer));return base64Encoder.encode(buffer);} catch (Exception e) {Log.e("TAG", "异常信息:" + e.getMessage());}return "";}
来源地址:https://blog.csdn.net/victor_E_N_01185/article/details/130274460