2
我正在開發一個Android應用程序,我需要下載MP3文件
但下載過程應該是在後臺。
對此有任何想法嗎?需要幫助的Android的MP3文件下載
我正在開發一個Android應用程序,我需要下載MP3文件
但下載過程應該是在後臺。
對此有任何想法嗎?需要幫助的Android的MP3文件下載
終於我找到了解決辦法。
public void mp3load() {
URL url = new URL(url);
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
String PATH = Environment.getExternalStorageDirectory()
+ "/download/";
Log.v(LOG_TAG, "PATH: " + PATH);
File file = new File(PATH);
file.mkdirs();
String fileName = "test.mp3";
File outputFile = new File(file, fileName);
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1);
}
fos.close();
is.close();
}
它的工作原理爲我好。
android: Is it possible to dowload file to disk given an xml (from a URL)?
檢查了這一點:有downloadFileFrom方法的AsyncTask類會幫助你。
非常奇怪。代碼對我來說只是一個小小的調整,但我覺得奇怪的是,它沒有設置任何權限,除了互聯網...... – Paramone 2014-01-22 11:29:25