How to Convert audio .mp3 file to String and vice versa?

流过昼夜 提交于 2019-12-08 17:00:45

use Base64.encodeToString() try this http://developer.android.com/reference/android/util/Base64.html#encodeToString%28byte%5B%5D,%20int%29

File file = new File(Environment.getExternalStorageDirectory() + "/hello-4.wav");
byte[] bytes = FileUtils.readFileToByteArray(file);

String encoded = Base64.encodeToString(bytes, 0);                                       
Utilities.log("~~~~~~~~ Encoded: ", encoded);

byte[] decoded = Base64.decode(encoded, 0);
Utilities.log("~~~~~~~~ Decoded: ", Arrays.toString(decoded));

try
{
    File file2 = new File(Environment.getExternalStorageDirectory() + "/hello-5.wav");
    FileOutputStream os = new FileOutputStream(file2, true);
    os.write(decoded);
    os.close();
}
catch (Exception e)
{
    e.printStackTrace();
}



public class FileUtils {

    /**
     * Instances should NOT be constructed in standard programming.
     */
    public FileUtils() { }

    /**
     * The number of bytes in a kilobyte.
     */
    public static final long ONE_KB = 1024;

    /**
     * The number of bytes in a megabyte.
     */
    public static final long ONE_MB = ONE_KB * ONE_KB;

    /**
     * The number of bytes in a gigabyte.
     */
    public static final long ONE_GB = ONE_KB * ONE_MB;



    public static String readFileToString(
            File file, String encoding) throws IOException {
        InputStream in = new java.io.FileInputStream(file);
        try {
            return IOUtils.toString(in, encoding);
        } finally {
            IOUtils.closeQuietly(in);
        }
    }



    }

}
Error:(49, 41) error: cannot find symbol method readFileToByteArray(File)

i think you have not defined that method in FileUtils, did you ?

i have resolved this issue by changing the code as follows:

//byte[] bytes = FileUtils.readFileToByteArray(file);
            byte[] bytes = new byte[(int)file.length()];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!