2013-04-09 48 views
0

我想從網頁服務器下載帶聲音URL的Mp3聲音。我不知道做什麼是最好的方法!我也看過這些問題。請引導我正確的方向。在Android中以編程方式從URL下載Mp3

Downaload a file programatically on android

What is best way to download files from net programatically in android?

+0

其中那些你試過嗎?你對這些樣品有什麼擔憂? – CommonsWare 2013-04-09 18:06:22

+0

我試過第一個!我想知道,如何將下載的原始數據保存爲sdcard的mp3音頻 – Desire 2013-04-09 18:07:49

回答

0

試試這個代碼,讓我知道

File output = new File(Environment.getExternalStorageDirectory(), 
    fileName); 
if (output.exists()) { 
    output.delete(); 
} 

InputStream stream = null; 
FileOutputStream fos = null; 
try { 

    URL url = new URL(urlPath); 
    stream = url.openConnection().getInputStream(); 
    InputStreamReader reader = new InputStreamReader(stream); 
    fos = new FileOutputStream(output.getPath()); 
    int next = -1; 
    while ((next = reader.read()) != -1) { 
    fos.write(next); 
    }