在浏览器如何打开本地的HTML页面 [英] how open local html page in browser

查看:2965
本文介绍了在浏览器如何打开本地的HTML页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做的,我想在浏览器中打开本地的HTML页面的应用程序。我可以打开google.com。但是,当我打开本地HTML文件。但我得到下面的错误

  

找不到请求的文件。

以下是我的code:

 尝试
    {
        意图I =新的意图(Intent.ACTION_VIEW);
        文件F =新的文件(文件:///android_asset/www/trialhtml.html);
        开放的我们的uri = Uri.fromFile(F);
        i.setClassName(com.android.browser,com.android.browser.BrowserActivity);
        i.setData(URI);
        startActivity(ⅰ);
    }
    赶上(例外五)
    {
      System.out.print(e.toString());

    }
 

解决方案

文件:///android_asset/www/trialhtml.html意味着什么到外部应用程序,如Web浏览器。 在你的资产的任何文件都不会被其他应用程序访问。你有两个选择。

  1. 复制html文件共享存储,以使该文件可以通过web浏览器进行访问。
  2. 实施中的新的活动或片段的WebView在应用程序中,然后 webview.loadUrl(文件:///android_asset/www/trialhtml.html);

您不需要阅读像其他答案的资产是指导你。的WebView会处理这一切的幕后,包括加载其他资产如图片

作为一个侧面说明,如果Web浏览器能够读取你的文件,你不会想使用

  i.setClassName(com.android.browser,com.android.browser.BrowserActivity);
 

这是因为你明确地要求一个特定的浏览器,其可以或可以不将用户的设备上安装。我有理由相信这不是说来,只有安装了Chrome浏览器的一些现代的Andr​​oid设备的情况。 正确的用法是像

 开放的URI = Uri.parse(http://www.example.com);
意向意图=新的意图(Intent.ACTION_VIEW,URI);
startActivity(意向);
 

由于没有明确设置类和包名,这将确保安装不管是哪个网页浏览器,用户默认会有所回升。

I am making an application in which i want to open a local html page in the browser. I am able to open google.com. But when i'm opening local html file. But i am getting following error

The Request file was not found.

Following is my code:

try
    {
        Intent i = new Intent(Intent.ACTION_VIEW);
        File f=new File("file:///android_asset/www/trialhtml.html");
        Uri uri = Uri.fromFile(f);
        i.setClassName("com.android.browser", "com.android.browser.BrowserActivity");
        i.setData(uri);
        startActivity(i);
    }
    catch(Exception e)
    {
      System.out.print(e.toString());   

    }

解决方案

file:///android_asset/www/trialhtml.html means nothing to an external application like the Web Browser. Any files in your assets are not accessible by other applications. You have 2 options.

  1. Copy the html file to shared storage so that the the file can be accessed by the webbrowser.
  2. Implement a WebView within a new Activity or fragment in your application then webview.loadUrl("file:///android_asset/www/trialhtml.html");

You do not need to read the asset like other answers are instructing you. WebView will handle this all behind the scenes, including loading other assets like images

As a side note, if the web browser was able to read your files, you would not want to use

        i.setClassName("com.android.browser", "com.android.browser.BrowserActivity");

This is because you are explicitly asking for a certain browser, which may or may not be installed on the user's device. I'm reasonably sure this isn't the case on some modern Android devices that come with only Chrome installed. The correct usage would be something like

Uri uri = Uri.parse("http://www.example.com"); 
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);

By not explicitly setting the class and package name, this ensures that no matter which web browser is installed, the users default will be picked.

这篇关于在浏览器如何打开本地的HTML页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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