我想從服務器中逐一下載所有mp3文件並保存到SD卡文件夾中。我沒有任何錯誤或例外,但MP3不下載並不顯示在SD卡中。有人可以幫助如何解決這個問題。這是我的代碼。從Android的SD卡中的服務器下載MP3文件
if (imageName.endsWith(mp3_Pattern))
{
str_DownLoadUrl = namespace + "/DownloadFile/FileName/" + imageName;
Log.e("######### ", "str_DownLoadUrl = " + str_DownLoadUrl);
download_Mp3File(str_DownLoadUrl);
strDownLoadStatus = "1";
dbhelper.update_DownLoadStatus(imageName, strDownLoadStatus);
}
void download_Mp3File(final String fileUrl) {
new AsyncTask<String, Integer, String>()
{
@Override
protected String doInBackground(String... arg0)
{
int count;
File file = new File(newFolder, System.currentTimeMillis() + imageName);
try
{
URL url = new URL(fileUrl);
URLConnection conexion = url.openConnection();
conexion.connect();
// this will be useful so that you can show a tipical 0-100% progress bar
int lenghtOfFile = conexion.getContentLength();
// downlod the file
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(file);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
publishProgress((int) (total * 100/lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
}
return null;
}
}.execute();
}
您沒有顯示'newFolder'的類型和值。請做。您的catch塊中沒有日誌。請添加它們,讓您知道發生了什麼。 LogCat中的任何錯誤/異常? – greenapps
讓doInBackground返回一個包含有用信息的字符串。實現onPostExecute可以記錄和烘烤字符串的內容。 – greenapps