简体   繁体   English

从应用程序(android 4.3)在浏览器中打开本地文件

[英]Open local file in browser from app (android 4.3)

I'm developing an app that must open a local file in browser. 我正在开发一个必须在浏览器中打开本地文件的应用程序。 In android versions less than 4.3 it works perfectly but in versions equal or above 4.3 the app always crashes. 在低于4.3的android版本中,它可以完美运行,但在等于或高于4.3的版本中,该应用始终会崩溃。 I think the problem is in Intent.ACTION_VIEW, but I dont have sure. 我认为问题出在Intent.ACTION_VIEW,但我不确定。

The code that I wrote is showed below. 我编写的代码如下所示。

Intent browserIntent = new Intent(Intent.ACTION_VIEW);
browserIntent.setClassName("com.android.browser", "com.android.browser.BrowserActivity");
browserIntent.setType("text/html");
browserIntent.setData(uri);
startActivity(browserIntent);

So the question is: how to open a local file in browser from app in android versions equal or above 4.3. 所以问题是:如何从等于或高于4.3的android版本中的应用中打开浏览器中的本地文件。

Remove the setClassName() . 删除setClassName() You should not assume what browser apps are installed on the device. 您不应假定设备上安装了哪些浏览器应用程序。 All it matters that you request that some user-chosen app handles the URI with the specified data type for you. 重要的是,您要求某些用户选择的应用程序为您处理具有指定数据类型的URI。

To be extra resilient, catch ActivityNotFoundException in case there's no activity to handle the intent (eg due to a restricted user profile). 为了更加灵活,请在没有活动可处理意图的情况下捕获ActivityNotFoundException (例如,由于受限的用户配置文件)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM