【问题标题】:Android: view local html file not showing all browsersAndroid:查看本地 html 文件不显示所有浏览器
【发布时间】:2013-09-29 06:37:06
【问题描述】:

我的应用程序正在生成一个 HTML 文件,然后我想将其显示给用户,我的代码如下 -

Uri uri = Uri.parse("file://" + fileName);
Intent browserIntent = new Intent(Intent.ACTION_VIEW);
browserIntent.setDataAndType(uri, "text/html");
browserIntent.addCategory(Intent.CATEGORY_BROWSABLE);
startActivity(browserIntent);

然后它向我显示“使用完成的操作”,但只列出了 FireFox 浏览器。我也安装了 Chrome、Opera 和 Dolphin 浏览器。为什么我不能全部选择?谢谢。

【问题讨论】:

  • 附注我删除了 "intent.setClassName("com.android.browser", "com.android.browser.BrowserActivity");"因为它会在某些设备上导致 ForceClose。

标签: android html android-intent


【解决方案1】:

我认为使用选择器可以使它们全部从一个意图中工作。到目前为止,我发现了 3 个略有不同的意图 -

    // chrome ??
    Intent intent1 = new Intent(Intent.ACTION_VIEW);        
    intent1.setDataAndType(uri, "multipart/related");

    // default "Internet" browser
    Intent intent2 = new Intent(Intent.ACTION_VIEW, uri);
    intent2.setDataAndType(uri, "text/html");
    intent2.setClassName("com.android.browser", "com.android.browser.BrowserActivity");         

    // any other browser (FireFox/HTML Viewer) ??
    Intent intent3 = new Intent(Intent.ACTION_VIEW);
    intent3.setDataAndType(uri, "text/html");
    intent3.addCategory(Intent.CATEGORY_BROWSABLE);  

使用此处提供的解决方案,可以将所有这些意图放入一个选择器中 - How to make an intent with multiple actions

我将 logcat 的回答视为已接受,因为它向我展示了我需要去的地方。谢谢。

【讨论】:

    【解决方案2】:

    您可以使用 root 手机,获取 Chrome apk,使用 apktool 查看清单。在那里你会看到 Chrome 通常只支持方案 http/https/about/javascript,并且文件方案在以下意图过滤器中只支持一次:

    <intent-filter>
      <action android:name="android.intent.action.VIEW"/>
      <category android:name="android.intent.category.DEFAULT"/>
      <data android:mimeType="multipart/related" android:scheme="file"/>
    </intent-filter>
    

    因此您可以尝试更改 mime 类型并对其他浏览器进行相同的调查。

    【讨论】:

    • 我将代码更改为 "browserIntent.setDataAndType(uri, "multipart/related");" & 删除了“browserIntent.addCategory(Intent.CATEGORY_BROWSABLE);”行并立即在 Chrome 中打开!我只需要确定所有其他浏览器需要什么。
    • 你可能没有那么幸运为所有浏览器创建一个意图。因此,对于不同的浏览器,您将有不同的意图,您可以通过 PackageManager.queryIntentActivities 查询它们中的每一个,然后将它们全部合并到自定义对话框中。
    • 是的,这就是我现在正在做的事情。感谢您为我指明正确的方向。
    【解决方案3】:

    那些其他应用程序大概不支持file:// 方案。无法保证设备将具有能够加载本地文件的浏览器。

    【讨论】:

    • 如果我在 Astro 文件管理器中单击 HTML 文件,我可以选择在 Dolphin、FireFox 或 HTML 查看器中打开。 Astro 一定在做一些我没有做的事情。
    【解决方案4】:

    根据你的申请结果,如果真的不需要,你可以做一个WebView并避免这个工作,选择一个浏览器来打开你的html文件。很简单,你可以用下面的代码创建一个web_file_reader.xml:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".WebActivity" >
    
        <WebView  xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/web_viewer1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>
    
        <Button
            android:id="@+id/but_leave"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:text="leave page" />
    
    </RelativeLayout>
    

    所以,在你的类 onCreate 回调方法上,做:

    protected void onCreate(Bundle savedInstanceState) 
    {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.relatorios_activity);
    
    Button leave = (Button) findViewById(R.id.but_leave);
    sair.setOnClickListener(new View.OnClickListener()
    {           
        @Override
        public void onClick(View v) 
        {
            finish();
        }
        });
    
        String fileURL = "file://" + filePath; //it's your file path name string
    
        WebView webView = (WebView) findViewById(R.id.wev_viewer1);
        webView.setVerticalScrollBarEnabled(true);
        webView.setHorizontalScrollBarEnabled(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.loadUrl(fileURL);       
    }
    

    设置一个按钮以从您的主要活动中打开以下意图:

    Intent browserView = new Intent(/*your main activity context*/,WebActivity.class);
    startActivity(browserView);
    

    这将打开具有任何布局和/或 java 脚本配置的任何 html 文件,具体取决于您在新 Activity 上的操作系统版本。

    【讨论】:

    • ps。添加javascript配置,设置傻瓜代码:webView.getSettings().setJavaScriptEnabled(true);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-11
    相关资源
    最近更新 更多