Android Webview下载并查看pdf,xls或doc文件 [英] Android webview download and view pdf, xls or doc files

查看:103
本文介绍了Android Webview下载并查看pdf,xls或doc文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个使用Webview导航特定网站的android应用.问题是我希望用户能够查看网站中的pdf,.xls或.doc文档.

I'm making an android app that uses a webview to navigate through a specific website. The thing is that I want users to be able to view pdf, .xls or .doc documents that are in the website.

我有这段代码,但是它打开了android浏览器,而不是查看pdf文件.

I have this code, but it opens the android browser instead of viewing the pdf file.

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);

    }
});

我还尝试使用 google docs查看器,但这也不起作用.它为我打开了链接的html,但没有打开文件!该网站不会将您定向到pdf的链接,而只能让您下载它,这就是Google doc查看器无法正常工作的原因.

Also, I tried using google docs viewer but this doesn't work either. It opens me the html of the link, it doesn't open the file! 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.

我想像android浏览器一样打开文件.它将下载文件,然后将其打开.我该怎么做?我以为使用android下载管理器,但是如何下载文件,打开文件以及在用户关闭文件时删除该文件?因为我只想下载临时时间.

I would like to open the file like the android browser. 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? because I would like to download only temporary time.

任何想法都欢迎.感谢您的宝贵时间!

Any ideas are welcome. Thanks for your time!

推荐答案

由于 Uri.parse(url)将为您提供一个网站地址,该地址通常分配给浏览器(因此将在其中打开).

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).

要下载文件,可以使用 DownloadManager -service 编写您自己的代码即可.将文件保存在临时位置后,可以使用上述方法将其打开.

To download the files, you can use the DownloadManager-service or write your own code to do so. Once you have the file in a temporary location, you can open it by using your above method.

如果要在用户查看文件后将其删除,一个想法是在您的活动 onResume() -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).

尽管如此,我还是希望将文件放在内部缓存并不时擦除它.Android也将自己做一些整理工作:

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可能会删除这些缓存文件以恢复空间.但是,您不应依赖在系统上为您清除这些文件.你应该总是自己维护缓存文件并保持在合理范围内消耗的空间,例如1MB.当用户卸载您的应用程序中,这些文件将被删除.

When the device is low on internal storage space, Android may delete these cache files to recover space. 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. When the user uninstalls your application, these files are removed.

这篇关于Android Webview下载并查看pdf,xls或doc文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆