Pra*_*mar 19 android file-copying android-sdcard
我的文件res/raw夹里有一些音频文件.出于某些原因,我想将这些文件复制到我的SDCard当我的应用程序启动时.
我怎么能这样做?有人指导我吗?
Nik*_*kov 43
从资源中读取,写入SD卡上的文件:
InputStream in = getResources().openRawResource(R.raw.myresource);
FileOutputStream out = new FileOutputStream(somePathOnSdCard);
byte[] buff = new byte[1024];
int read = 0;
try {
while ((read = in.read(buff)) > 0) {
out.write(buff, 0, read);
}
} finally {
in.close();
out.close();
}
Run Code Online (Sandbox Code Playgroud)
这是我用来完成这项工作的方法,此方法接收资源 ID 和存储所需的名称,例如:
copyFiletoExternalStorage(R.raw.mysound, "jorgesys_sound.mp3");
Run Code Online (Sandbox Code Playgroud)
方法:
private void copyFiletoExternalStorage(int resourceId, String resourceName){
String pathSDCard = Environment.getExternalStorageDirectory() + "/Android/data/" + resourceName;
try{
InputStream in = getResources().openRawResource(resourceId);
FileOutputStream out = null;
out = new FileOutputStream(pathSDCard);
byte[] buff = new byte[1024];
int read = 0;
try {
while ((read = in.read(buff)) > 0) {
out.write(buff, 0, read);
}
} finally {
in.close();
out.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17033 次 |
| 最近记录: |