[英]Opening a file downloaded by download manager
I am using the DownloadManager
to download files from a Webserver. 我正在使用DownloadManager
从Web服务器下载文件。
First I create the DownloadManager.Request
, add the headers to it, add the data to it (title, description, notification, MimeType) and then enqueue it. 首先,我创建DownloadManager.Request
,向其添加标题,向其添加数据(标题,描述,通知,MimeType),然后使其入队。
After that I wait until the file finished downloading, get the uri
and then create an intent to open the file. 之后,我等待文件下载完成,获取uri
,然后创建一个意图来打开文件。
If I want to open the file (PDF or txt) via a chosen program, I tried with Google Pdf Viewer, HTML Viewer, Chrome and others, it always tells me the file can't be opened. 如果我想通过选定的程序打开文件(PDF或txt),我尝试使用Google Pdf Viewer,HTML Viewer,Chrome和其他程序,它总是告诉我无法打开文件。 When I want to open it via the top bar DownloadManager
Notification though, the file opens correctly. 当我想通过顶部栏DownloadManager
Notification打开它时,文件正确打开。
public void getFileContent(Map<String, String> headers) {
if (downloadManager != null) {
DownloadManager.Request request = getRequest(headers);
Long fileId = downloadManager.enqueue(request);
compositeSubscription.add(RxDownloader.getInstance(getActivity())
.download(request)
.subscribe(path -> showFileContent(Uri.parse(path)),
throwable -> showError(throwable.getMessage())));
}
}
private DownloadManager.Request getRequest(Map<String, String> headers) {
Uri uri = Uri.parse(BuildConfig.API_URL + "api/v2/files/" + fileResource.getId() + "/raw");
DownloadManager.Request request = new DownloadManager.Request(uri);
request = addRequestHeaders(request, headers);
request = setRequestData(request);
return request;
}
private DownloadManager.Request addRequestHeaders(DownloadManager.Request request, Map<String, String> headers) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
request.addRequestHeader(entry.getKey(), entry.getValue());
}
return request;
}
private DownloadManager.Request setRequestData(DownloadManager.Request request) {
request.setTitle(getString(R.string.file_downloader));
request.setDescription(String.format(getString(R.string.fmt_downloading), fileResource.getFileName()));
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setMimeType(fileResource.getMimeType());
return request;
}
private void showFileContent(Uri uri) {
Intent target = new Intent(Intent.ACTION_VIEW);
target.setDataAndType(uri, fileResource.getMimeType());
target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Intent intent = Intent.createChooser(target, getString(R.string.open_file));
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
showError(getString(R.string.no_application_installed));
}
}
Ok, I solved the problem by setting a saving destination in the external public dir for the request, since the default download destination is a shared volume where the system might delete your file if it needs to reclaim space for system use. 好的,我通过在请求的外部公共目录中设置保存目标来解决了该问题,因为默认的下载目标是共享卷,如果需要回收空间以供系统使用,系统可能会删除您的文件。 https://developer.android.com/reference/android/app/DownloadManager.Request.html https://developer.android.com/reference/android/app/DownloadManager.Request.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.