2012-02-29 64 views
0

嗨,大家好我在資產文件夾中有一個音頻文件,我需要將相同的文件保存到SD卡上。將原始或資產文件夾中的音頻文件保存到SD卡Android

如何實現這一目標。

下面是我使用的保存文件的代碼

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 
} 

請幫助

回答

2

試試這個

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

轉換成字節數組複製此到SD卡後如下

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 
} 
+0

thanks thanks試着回到你身邊 – Goofy 2012-02-29 05:34:54

+0

嘿這是什麼baf? – Goofy 2012-02-29 05:37:06

+0

ByteArrayBuffer baf = new ByteArrayBuffer(1024); – user1203673 2012-02-29 05:38:23

相關問題