简体   繁体   English

Android Webview下载并查看pdf,xls或doc文件

[英]Android webview download and view pdf, xls or doc files

I'm making an android app that uses a webview to navigate through a specific website. 我正在制作一个使用网络视图浏览特定网站的android应用。 The thing is that I want users to be able to view pdf, .xls or .doc documents that are in the website. 问题是我希望用户能够查看网站中的pdf,.xls或.doc文档。

I have this code, but it opens the android browser instead of viewing the pdf file. 我有这段代码,但是它打开了android浏览器,而不是查看pdf文件。

webView.setDownloadListener(new DownloadListener() {
    public void onDownloadStart(String url, String userAgent,
            String contentDisposition, String mimetype, long contentLength) {
        Uri uri = Uri.parse(url);
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        startActivity(intent);

    }
});

Also, I tried using google docs viewer but this doesn't work either. 另外,我尝试使用Google Docs Viewer,但这也不起作用。 It opens me the html of the link, it doesn't open the file! 它为我打开了链接的html,但没有打开文件! The website doesn't direct you to the link of the pdf, it only let you download it, that's why google doc viewer doesn't work. 该网站不会将您定向到pdf的链接,而只能让您下载它,这就是Google doc查看器无法正常工作的原因。

I would like to open the file like the android browser. 我想像android浏览器一样打开文件。 It downloads the file and then it opens it. 它下载文件,然后将其打开。 How can I accomplish this? 我该怎么做? I thought using android download manager, but how do I download the file, open it and when the user close it delete that file? 我以为使用android下载管理器,但是如何下载文件,打开文件,以及当用户关闭文件时删除该文件? because I would like to download only temporary time. 因为我只想下载临时时间。

Any ideas are welcome. 任何想法都欢迎。 Thanks for your time! 谢谢你的时间!

You'll have to download the files first, so Android can find an appropriate application to open them with, since Uri.parse(url) will give you a website address, which is normally assigned to the browser (and will therefore open in it). 您必须先下载文件,以便Android可以找到合适的应用程序来打开文件,因为Uri.parse(url)将为您提供一个网站地址,该地址通常分配给浏览器(因此将在浏览器中打开) )。

To download the files, you can use the DownloadManager -service or write your own code to do so. 要下载文件,可以使用DownloadManager -service编写自己的代码来进行DownloadManager Once you have the file in a temporary location, you can open it by using your above method. 将文件保存在临时位置后,可以使用上述方法将其打开。

If you want to delete the file after the user viewed it, one idea would be to do it in your activities onResume() -method, which will be called when the user navigates back from viewing the file (using the back-button). 如果要在用户查看文件后删除文件,一个想法是在您的活动onResume()方法中执行该操作,当用户从查看文件的位置导航回来(使用后退按钮)时,将调用该方法。

Although, I would rather put the files in the internal cache and wipe it every now and then. 虽然,我宁愿将文件放在内部缓存中并时不时擦除它。 Android will do a little house-keeping itself, too: Android也将自己做一些整理工作:

When the device is low on internal storage space, Android may delete these cache files to recover space. 当设备的内部存储空间不足时,Android可能会删除这些缓存文件以恢复空间。 However, you should not rely on the system to clean up these files for you. 但是, 您不应该依赖系统来为您清理这些文件。 You should always maintain the cache files yourself and stay within a reasonable limit of space consumed, such as 1MB. 您应该始终自己维护高速缓存文件,并保持在合理的空间消耗限制内,例如1MB。 When the user uninstalls your application, these files are removed. 当用户卸载您的应用程序时,这些文件将被删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在android webview中下载文件(pdf,ppt,xls)以下载没有浏览器的文件夹 - Download files (pdf , ppt , xls) in android webview to download folder without browser 在android中从sdcard到webview打开doc,xls,pdf等文件 - Open file such as doc,xls,pdf from sdcard to webview in android Android Webview 下载管理器下载 PDF 文件 - Android Webview Download manager To Download PDF Files 使用phonegap在android和ios中读取pdf,doc,ppt和xls文件 - Reading Pdf, Doc, Ppt and xls files in android and ios using phonegap 在Android Webview中查看PDF文件 - View PDF files in Android webview 以编程方式查看/显示Android中的.doc,docx,xl​​s文件 - View/display .doc,docx,xls files in Android programmatically 我无法显示ppt实际上,我不仅要在视图中显示ppt,还要显示doc / ppt / pdf / xls文件…在android中 - Iam not able to display the ppt infact i want to display not only ppt but also doc/ppt/pdf/xls files in a view… in android 如何在Web视图中使用下载管理器将pdf / ppt / doc文件下载到sdcard - How to use a download Manager in a webview to download pdf/ppt/doc files to sdcard 使用Android Webview下载文件 - Download files with Android Webview 将doc,pdf,xls等文件从android应用程序上传到php服务器 - Upload doc, pdf,xls etc, from android application to php server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM