我有這個代碼片斷中,我要處理的文件立即下載被點擊:手動處理MP3下載的WebView
else if (url.startsWith("http://rapahh.com/songs2/Music%20Promotion/Download/")) {
}
return false;
雖然我不知道如何處理Android的下載,這樣做任何人都有我可以使用的代碼片段在後臺下載文件到一個文件夾..下載文件夾是好的。謝謝。
我有這個代碼片斷中,我要處理的文件立即下載被點擊:手動處理MP3下載的WebView
else if (url.startsWith("http://rapahh.com/songs2/Music%20Promotion/Download/")) {
}
return false;
雖然我不知道如何處理Android的下載,這樣做任何人都有我可以使用的代碼片段在後臺下載文件到一個文件夾..下載文件夾是好的。謝謝。
你正在爲什麼版本的android?
從API lvl 9開始,有DownloadManager可以爲您處理。如果可能,您應該使用DownloadManager,因爲它會自動處理網絡中斷併爲您恢復下載。
如果您的目標是降低API級別,那麼您必須自己下載代碼。你將有一個來自你的Web源的inputStream和一個outputStream去你的本地文件,你將通過inputStream循環寫入數據塊,直到沒有剩下的。 事情是這樣的:
try {
URL url = new URL(URL); //URL of the video
//Set our file to the correct path and name.
File file = new File(PATH + fileName);
//keep the start time so we can display how long it took to the Log.
long startTime = System.currentTimeMillis();
Log.d(myTag, "download begining");
//Log.d(myTag, "download url:" + url);
Log.d(myTag, "downloaded file name:" + fileName);
/* Open a connection to that URL. */
URLConnection ucon = url.openConnection();
// this will be useful so that you can show a tipical 0-100% progress bar
int lenghtOfFile = ucon.getContentLength();
Log.i(myTag, "Opened Connection");
/************************************************
* Define InputStreams to read from the URLConnection.
************************************************/
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
Log.i(myTag, "Got InputStream and BufferedInputStream");
/************************************************
* Define OutputStreams to write to our file.
************************************************/
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
Log.i(myTag, "Got FileOutputStream and BufferedOutputStream");
/************************************************
* Start reading the and writing our file.
************************************************/
byte data[] = new byte[1024];
long total = 0;
int count;
//loop and read the current chunk
while ((count = bis.read(data)) != -1) {
//Post our progress update back to the UI thread
postProgress((int)(total*100/lenghtOfFile));
//write this chunk
total += count;
bos.write(data, 0, count);
}
//Have to call flush or the video file can get corrupted and won't play correctly.
bos.flush();
bos.close();
Log.d(myTag, "download ready in "
+ ((System.currentTimeMillis() - startTime))
+ " milisec");
} catch (IOException e) {
Log.d(myTag, "Error: " + e);
}
你需要實現postProgress(int progress)
方法做任何適合您的應用程序,告知有多少比例的用戶完成下載。
編輯:
您可以註釋掉日誌以使其正常工作。雖然爲了簡化流程,但我在調試過程中將其留在了原位。日誌語句如Log.i(String tag, String text)
類似於System.out.println(String txt)
不同之處在於這些語句被打印到日誌文件中(您可以在eclipse中的DDMS透視圖中看到它們)並且它們有一個名爲「tag」的附加參數,您可以將它傳遞給任何字符串你喜歡,這個字符串會顯示在日誌文件的文本旁邊。您也可以在DDMS透視圖中過濾基於這些標籤的日誌輸出。通常的做法是將標籤聲明爲靜態字符串,以便您可以將該引用用於所有日誌語句,並保證始終具有相同的標記。所以,如果你添加類似這樣的類到你的班級,它應該修復你的錯誤:
final static String myTag = "NameOfYourActivity";
我的目標是降低API。 Log.d的myTag部分總是給我錯誤,我會把它放在一個私有類中,然後在我的if語句中執行它? –
您必須使用自己的標記字符串,或者聲明myTag變量以按原樣使用代碼。這更多的是讓你指出正確的方向,而不是100%功能,沒有變化。看我的編輯。 – FoamyGuy
好的,謝謝,我可以在我的if語句中輸入該語句,還是必須是我在if語句中執行的私人類? –