後的研究2天后,我沒有發現任何解決我的問題:/安卓:從URL下載內容返回MP3文件
我想下載一個MP3文件,該文件被調用後纔會返回瀏覽器中的URL。
我不能給你實際的URL,因爲我不允許由於版權限制,但它的形式是:
http://wscompany.name.com/downloadws/getDlFile/mdkHdKy97RppVWOsIOdDBuG/audio/1478
正如你可以看到,這個URL沒有mp3擴展。所以如果我在窗口上的瀏覽器中放入這種URL,它會返回一個MP3以保存到磁盤上,這很好。但如果我想在android中調用此URL來下載最終返回的文件(mp3),它不起作用。
我試着用包含一個mp3文件的url直接和它工作得很好(如http://www.mediacollege.com/downloads/sound-effects/urban/factory/Factory_External_01.mp3),但沒有沒有mp3擴展名的網址,即使它確實返回一個mp3,我希望你明白我的意思。
有誰知道如何在android中做到這一點?
這裏是我的代碼,使用的AsyncTask,通過
new Download(MyActivity.this, urlToCall).execute();
,下載的AsyncTask叫:
public class Download extends AsyncTask<String, Void, String>
{
ProgressDialog mProgressDialog;
Context context;
String urlDownload;
public Download(Context context,String url)
{
this.context = context;
this.urlDownload=url;
}
protected void onPreExecute()
{
mProgressDialog = ProgressDialog.show(context, "","Please wait, Download for " + urlDownload);
Log.v("DOWNLOAD", "Wait for downloading url : " + urlDownload);
}
protected String doInBackground(String... params)
{
try
{
//URL url = new URL("http://www.mediacollege.com/downloads/sound-effects/urban/factory/Factory_External_01.mp3");
URL url = new URL(urlDownload);
Log.w("DOWNLOAD" , "URL TO CALL : " + url.toString());
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
//set up some things on the connection
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.connect();
File folder = new File(Environment.getExternalStorageDirectory()+ "/MyDownloaded/") ;
boolean success = true;
if (!folder.exists()) {
success = folder.mkdir();
}
File file = new File(folder,"somefile.mp3");
FileOutputStream fileOutput = new FileOutputStream(file);
InputStream inputStream = urlConnection.getInputStream();
//this is the total size of the file
int totalSize = urlConnection.getContentLength();
//variable to store total downloaded bytes
int downloadedSize = 0;
//create a buffer...
byte[] buffer = new byte[1024];
int bufferLength = 0; //used to store a temporary size of the buffer
//now, read through the input buffer and write the contents to the file
while ((bufferLength = inputStream.read(buffer)) > 0) {
//add the data in the buffer to the file in the file output stream (the file on the sd card
fileOutput.write(buffer, 0, bufferLength);
//add up the size so we know how much is downloaded
downloadedSize += bufferLength;
//this is where you would do something to report the prgress, like this maybe
//updateProgress(downloadedSize, totalSize);
Log.w("DOWNLOAD" , "progress " + downloadedSize + "/" + totalSize);
}
//close the output stream when done
fileOutput.close();
//catch some possible errors...
}
catch (MalformedURLException e)
{
Log.e("DOWNLOAD" , "ERROR : " + e);
}
catch (IOException e)
{
Log.e("DOWNLOAD" , "ERROR : " + e);
}
return "done";
}
private void publishProgress(int i)
{
Log.v("DOWNLOAD", "PROGRESS ... " + i);
}
protected void onPostExecute(String result)
{
if (result.equals("done"))
mProgressDialog.dismiss();
}
謝謝你在前進,我希望有人能幫助我:)
DevJ
請在評論中寫下downvoting的原因。像雞一樣降低和隱藏是不公平的。 – 2013-05-14 07:41:49
我同意,我沒有看到這個問題沒有錯(看起來英語不是OP的原始語言,但我們應該始終努力嘗試理解,並始終留下評論爲什麼) – 2013-05-14 07:45:26
對不起,我的英語差..我做了很多努力說好.. – DevJ 2013-05-14 07:49:32