save audio file in raw or assets folder to sdcard android

ⅰ亾dé卋堺 提交于 2019-12-08 12:16:10

问题


Hi guys i am having a audio file in assets folder and i need to save the same file onto sdcard .

How to acheive this.

Below is the code i am using to save file

String filename = "filename.txt";
File file = new File(Environment.getExternalStorageDirectory(), filename);
FileOutputStream fos;
byte[] data = new String("data to write to file").getBytes();
try {
    fos = new FileOutputStream(file);
    fos.write(data);
    fos.flush();
    fos.close();
} catch (FileNotFoundException e) {
    // handle exception
} catch (IOException e) {
    // handle exception
}

please help


回答1:


try this

AssetManager mngr = getAssets();
InputStream path = mngr.open("music/music1.mp3"); 

                    BufferedInputStream bis = new BufferedInputStream(path,1024);

                    //get the bytes one by one
                    int current = 0;

                    while ((current = bis.read()) != -1) {

                        baf.append((byte) current);
                    }
}
byte[] bitmapdata  = baf.toByteArray();

After converting into byte array copy this into the sdcard as follows

File file = new File(Environment.getExternalStorageDirectory(), music1.mp3);
FileOutputStream fos;

try {
    fos = new FileOutputStream(file);
    fos.write(bitmapdata  );
    fos.flush();
    fos.close();
} catch (FileNotFoundException e) {
    // handle exception
} catch (IOException e) {
    // handle exception
}


来源:https://stackoverflow.com/questions/9493983/save-audio-file-in-raw-or-assets-folder-to-sdcard-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!