android 视频文件转base64
时间: 2023-12-06 13:35:08 浏览: 200
android录制视频并转为Base64格式
3星 · 编辑精心推荐
以下是两种Android视频文件转Base64的方法:
```java
//方法一:需要导入Jar包
/**
* 文件转Base64.
* @param path
* @return
* @throws Exception
*/
public static String fileToBase64(String path) throws Exception {
File file = new File(path);
FileInputStream inputFile = new FileInputStream(file);
byte[] buffer = new byte[(int) file.length()];
inputFile.read(buffer);
inputFile.close();
return new BASE64Encoder().encode(buffer);
}
```
```java
//方法二:不需要导入Jar包
/**
* 文件转Base64.
* @param filePath
* @return
*/
public static String file2Base64(String filePath) {
FileInputStream objFileIS = null;
try {
objFileIS = new FileInputStream(filePath);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
ByteArrayOutputStream objByteArrayOS = new ByteArrayOutputStream();
byte[] byteBufferString = new byte[1024];
try {
for (int readNum; (readNum = objFileIS.read(byteBufferString)) != -1; ) {
objByteArrayOS.write(byteBufferString, 0, readNum);
}
} catch (IOException e) {
e.printStackTrace();
}
String videodata = Base64.encodeToString(objByteArrayOS.toByteArray(), Base64.DEFAULT);
return videodata;
}
```
阅读全文