Android DownloadManager API - 下载后打开文件?
我遇到通过 DownloadManager API 成功下载后打开下载文件的问题。在我的代码中:
Uri uri=Uri.parse("http://www.nasa.gov/images/content/206402main_jsc2007e113280_hires.jpg");
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
.mkdirs();
lastDownload = mgr.enqueue(new DownloadManager.Request(uri)
.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI |
DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false)
.setTitle("app update")
.setDescription("New version 1.1")
.setShowRunningNotification(true)
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "a.apk"));
Cursor c=mgr.query(new DownloadManager.Query().setFilterById(lastDownload));
if(c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS)) == 8) {
try {
mgr.openDownloadedFile(c.getLong(c.getColumnIndex(DownloadManager.COLUMN_ID)));
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.d("MGR", "Error");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.d("MGR", "Error");
}
}
问题是何时调用 if(c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS))==8)
。我的状态为 -1 并有一个例外。有没有更好的方法,如何使用 DownloadManager API
打开下载的文件?在我的示例中,我正在下载一个大图像,在实际情况下,我将下载一个 APK
文件,并且我需要在 udpate 后立即显示安装对话框。
编辑:我发现 status=8 是在成功下载之后。您可能有不同的“检查成功下载”方法
谢谢
I am facing problem of opening downloaded file after successfull download via DownloadManager API. In my code:
Uri uri=Uri.parse("http://www.nasa.gov/images/content/206402main_jsc2007e113280_hires.jpg");
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
.mkdirs();
lastDownload = mgr.enqueue(new DownloadManager.Request(uri)
.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI |
DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false)
.setTitle("app update")
.setDescription("New version 1.1")
.setShowRunningNotification(true)
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "a.apk"));
Cursor c=mgr.query(new DownloadManager.Query().setFilterById(lastDownload));
if(c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS)) == 8) {
try {
mgr.openDownloadedFile(c.getLong(c.getColumnIndex(DownloadManager.COLUMN_ID)));
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.d("MGR", "Error");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.d("MGR", "Error");
}
}
Problem is when is if(c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS))==8)
evoked. I got status -1 and an exception. Is there any better way, how to open downloaded files with DownloadManager API
? In my example I am downloading a big image, in a real situation I would be downloading an APK
file and I need to display an installation dialog immediately after udpate.
Edit: I figured out that status=8 is after successfull download. You might have different "checking successfull download" approach
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
问题
Android DownloadManager API - 下载后打开文件?
解决方案
初始化FileProvider详细信息
在AndroidManifest中清除FileProvider
添加以下文件“res -> xml -> file_path.xml”< /strong>
注意
为什么使用 FileProvider
使用“DownloadManager.getUriForDownloadedFile(long id)”的问题
参考
Problem
Android DownloadManager API - opening file after download?
Solution
Initialize FileProvider Details
Decleare FileProvider in AndroidManifest
Add the following file "res -> xml -> file_path.xml"
Note
Why Use FileProvider
Provblem with using "DownloadManager.getUriForDownloadedFile(long id)"
Reference
下载完成后,您需要注册一个接收器:
和一个 BroadcastReciever 处理程序
Buy 而不是我抄袭所有内容,我建议您检查 这个。
编辑:
作为一个建议,我还不建议使用 API 9:
http://developer.android.com/resources/dashboard/platform-versions.html
有很多方法可以解决这个问题,通过创建你自己的下载处理程序,就像我所做的那样,因为我们不想疏远我们的大多数 Android 用户群,为此你需要:
创建处理文件下载的 AsyncTask 。
我会建议创建某种下载对话框(如果你说它是一个大文件,我会让它出现在通知区域中)。
然后您需要处理文件的打开:
You need to register a reciever for when the download is complete:
and a BroadcastReciever handler
Buy instead of me ripping off everything, I suggest you'll check this out.
EDIT:
Just as a suggestion, I wouldn't recommend using API 9 just yet:
http://developer.android.com/resources/dashboard/platform-versions.html
There are ways around this, by creating your very own download handler, like I did, because we didn't want to alienate most of our android's user base, for that you'll need:
Create AsyncTask which handles the file download.
and i'll recommend to create a download dialog of some sort (if you say it's a big file, i'd make it appear in the notification area).
and than you'll need to handle the opening of the file:
对我来说,帮助添加了
Intent.FLAG_GRANT_READ_URI_PERMISSION
,我已经忘记了For me, helped adding
Intent.FLAG_GRANT_READ_URI_PERMISSION
,which i have forgotten对于 Kotlin,您可以轻松地使用
URL.openStream()
方法来读取文件并将其保存在目录中。如果你想做更多更奇特的事情,比如后台线程。
您应该查看 Elye 在 Medium 上的文章。
https://medium.com/移动应用开发发布/download-file-in-android-with-kotlin-874d50bccaa2
For Kotlin, you can easily just use the
URL.openStream()
method to read and save your file in your directory.If you want to do more fancier, like background threads.
You should checkout Elye's article on Medium.
https://medium.com/mobile-app-development-publication/download-file-in-android-with-kotlin-874d50bccaa2
请记住将
添加到您的 AndroidMannifest.xml 文件中remember add
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
to your AndroidMannifest.xml file