我在我的一个Android项目中使用Download Manager。这是我从URL下载歌曲的代码。
private void DownloadTask(String downloadUrl){
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(downloadUrl));
request.setDescription("Testando");
request.setTitle("Download");
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "test.mp3");
final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
final long downloadId = manager.enqueue(request);
showDownloadProgressDialog("Downloading", DownloadManagerActivity.this);
new Thread(new Runnable() {
@Override
public void run() {
boolean downloading = true;
while (downloading) {
DownloadManager.Query q = new DownloadManager.Query();
q.setFilterById(downloadId);
Cursor cursor = manager.query(q);
cursor.moveToFirst();
int bytes_downloaded = cursor.getInt(cursor
.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
int bytes_total = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL) {
downloading = false;
dismissDialog();
}
final int dl_progress = (int) ((bytes_downloaded * 100l) / bytes_total);
runOnUiThread(new Runnable() {
@Override
public void run() {
mProgressBar.setProgress((int) dl_progress);
}
});
Log.d("Status", statusMessage(cursor));
cursor.close();
}
}
}).start();
}
下载包含大多数网址的歌曲时效果很好,但是无法使用以下网址 http://pleer.com/browser-extension/files/5353392s90O.mp3
我曾尝试使用AsyncTask下载同一首歌,它就像一个魅力。这是它的代码
URL url = new URL(params[0]);
URLConnection conexion = url.openConnection();
conexion.connect();
int lenghtOfFile = conexion.getContentLength();
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+MusicConstants.FOLDERNAME+"/"+downloadingSongName+".mp3");
Log.e("File Saved at==>",Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+MusicConstants.FOLDERNAME+"/"+downloadingSongName+".mp3");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress(""+(int)((total*100)/lenghtOfFile));
output.write(data, 0, count);
}
song.setSongDownloadedPath(ExternalStorage.getSdCardPath()+MusicConstants.FOLDERNAME+"/"+downloadingSongName+".mp3");
Log.e("File Saved at==>",ExternalStorage.getSdCardPath()+MusicConstants.FOLDERNAME+"/"+downloadingSongName);
long success=db.addSongInDatabase(song);
output.flush();
output.close();
input.close();
下载管理器应该是什么问题?这是URL的问题吗?任何帮助将不胜感激。
答案 0 :(得分:0)
下载管理器会导致一些复杂的Urls问题,只需尝试添加这三行
downloadUrl = downloadUrl.replace(" ", "%20");
downloadUrl = downloadUrl.replace("[", "%5B");
downloadUrl = downloadUrl.replace("]", "%5D");
以上
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(downloadUrl));
在你的源代码中