您的位置:首页 > 移动开发 > Android开发

Android audio transmit with Base64 based on XMPP

2016-05-19 17:16 387 查看

Base64 encode:

private String filePathToString(String filePath)
{
byte[] audioBytes;

String audioString="";
try
{
ByteArrayOutputStream baos=new ByteArrayOutputStream();
FileInputStream fis= null;
fis = new FileInputStream(new File(filePath));
byte[] buf=new byte[1024];
int n;
while (-1!=(n=fis.read(buf)))
baos.write(buf,0,n);
audioBytes=baos.toByteArray();

audioString= Base64.encodeToString(audioBytes, Base64.DEFAULT);

} catch (Exception e)
{
e.printStackTrace();
}
return audioString;

}


Base64 decode:

private String stringToFilePath(String fileString) throws IOException {
byte[] bytesFile=Base64.decode(fileString,Base64.DEFAULT);
String decodedString=new String(bytesFile);
File file = null;
try
{
String fileName=generateFileName();
file = new File(Environment.getExternalStorageDirectory() + "/"+fileName);
FileOutputStream fos = new FileOutputStream(file, true);
fos.write(bytesFile);
fos.close();

}catch (Exception e)
{
e.printStackTrace();
}
return file.getAbsolutePath();

}

Reference:

How to get an audio file and encode to base64 string
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: