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();
}