HY。我正在开发一个Android应用程序,我从服务器下载视频。我同时下载多个视频并将其保存在SD卡中没有任何问题。我正在使用DownloadManager类进行下载。我可以在状态栏中看到下载图标并在通知区域中下载进度,它由DownloadManger本身管理。我有一个单独的选项卡名称下载,我使用listview显示以前下载的视频,但现在我还必须显示当前正在下载的带有进度条的视频。我的问题是 1 - 如何将当前正在下载的视频显示在列表视图中 2 - 如何维护所有视频的进度条。
这是我目前的代码
String urlDownload = "https://dl.dropbox.com/s/ex4clsfmiu142dy/test.zip?token_hash=AAGD-XcBL8C3flflkmxjbzdr7_2W_i6CZ_3rM5zQpUCYaw&dl=1";
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(urlDownload));
request.setDescription("Testando");
request.setTitle("Download");
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "teste.zip");
final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
final long downloadId = manager.enqueue(request);
final ProgressBar mProgressBar = (ProgressBar) findViewById(R.id.progressBar1);
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;
}
final int dl_progress = (int) ((double)bytes_downloaded / (double)bytes_total * 100f);
runOnUiThread(new Runnable() {
@Override
public void run() {
mProgressBar.setProgress((int) dl_progress);
}
});
Log.d(Constants.MAIN_VIEW_ACTIVITY, statusMessage(cursor));
cursor.close();
}
}
}).start();
答案 0 :(得分:0)
如果您想要显示卡片或存储空间中的所有视频,可以参考野兔 android scanning all .mp3 files in SD Card 对于当前视频,您必须将当前视频存储在任何任何sql或共享首选项文件中,您可以在完成从卫星或共享首选项中删除后从中获取名称