我使用下载管理器下载.ogg文件。 我使用以下代码进行下载:
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription(list.get(position).getName());
request.setTitle(list.get(position).getName());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalFilesDir(context, Environment.DIRECTORY_DOWNLOADS, list.get(position).getName() + ".ogg");
DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
现在我想共享此文件,所以我需要他的uri。
如何获取此文件uri?
答案 0 :(得分:0)
方法DownloadManager.enqueue()
返回一个ID(DOC IS HERE)。因此,您必须“存储”该ID以便以后进行操作(例如查询下载文件的Uri)。
这样,如果使用Uri
(DOC HERE)成功下载了文件,则可以使用该ID来获取Uri getUriForDownloadedFile(long id);
编辑
如果您创建了BroadcastReceiver
来接收有关已完成下载的广播,则在文件下载后立即可以获取Uri:
private int mFileDownloadedId = -1;
// Requesting the download
DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
mFileDownloadedId = manager.enqueue(request);
// Later, when you receive the broadcast about download completed.
private BroadcastReceiver onDownloadComplete = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
if (downloadedID == mFileDownloadedId) {
// File received
DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = manager.getUriForDownloadedFile(mFileDownloadedId);
}
}
}