Android audio transmit with Base64 based on XMPP

来源:互联网 发布:中国 种族歧视 知乎 编辑:程序博客网 时间:2024/06/06 05:17

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


0 0