如何在浏览器中打开本地html页面

时间:2013-02-08 13:10:49

标签: android browser

我正在创建一个应用程序,我想在浏览器中打开一个本地html页面。我可以打开google.com。但是当我打开本地html文件时。但我得到以下错误

  

找不到请求文件。

以下是我的代码:

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

    }

5 个答案:

答案 0 :(得分:4)

file:///android_asset/www/trialhtml.html对Web浏览器等外部应用程序没有任何意义。 其他应用程序无法访问您资产中的任何文件。你有2个选择。

  1. 将html文件复制到共享存储,以便webbrowser可以访问该文件。
  2. 然后在应用程序中的新Activity或片段中实现WebView webview.loadUrl("file:///android_asset/www/trialhtml.html");
  3. 您不需要像其他答案指示您一样阅读资产。 WebView将在幕后处理所有这些,包括加载其他资产,如图像

    作为旁注,如果网络浏览器能够读取您的文件,您将不想使用

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

    这是因为您明确要求某个浏览器,可能会也可能不会安装在用户的设备上。我有理由相信,在一些只安装了Chrome的现代Android设备上并非如此。 正确的用法就像是

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

    通过不明确设置类和包名称,这可以确保无论安装哪个Web浏览器,都将选择用户默认值。

答案 1 :(得分:1)

以原始资源的形式打开并加载,然后将WebView放入loadData

InputStream page = getResources()
   .openRawResource(R.raw.my_web_page);
 BufferedReader r = new BufferedReader(new InputStreamReader(page));
String s;
while ((s = r.readLine()) != null)
  builder.append(s);
r.close();
myWebView.loadData(builder.toString(), "text/html", "UTF-8");

只有您自己的应用程序才能轻松阅读您的资产。

如果您需要使用外部浏览器打开文件,请将获取的字符串保存到外部浏览器也可以访问的公共位置。

答案 2 :(得分:0)

您需要使用Android Assetmanager来读取资产中的文件

AssetManager am = context.getAssets();
InputStream is = am.open("trialhtml.html");

答案 3 :(得分:0)

尝试使用webView,您可以打开它。如果你在html中使用java脚本文件,那么你必须添加

  

webView.getSettings()setJavaScriptEnabled(真);

进入您的活动,并将javascript文件添加到assest / www文件夹中。

webView = (WebView)findViewById(R.id.webView);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebChromeClient(new WebChromeClient());
        webView.loadUrl("file:///android_asset/www/index.html");

答案 4 :(得分:0)

将html字符串保存为文件:

 public File saveStringToHtmlFile(String htmlString) {
        File htmlFile = null;
        try {
            // If the file does not exists, it is created.
            htmlFile = new File((ContextUtils.getAppContext()).getExternalFilesDir(null), "HTML_FILE_NAME.html");
            if (!htmlFile.exists()) {
                htmlFile.createNewFile();
            }

            // Adds a line to the file
            BufferedWriter writer = new BufferedWriter(new FileWriter(htmlFile, false));
            writer.write(htmlString);
            writer.close();
        } catch (IOException e) {
            Log.e(TAG, "Unable to write to HTML_FILE_NAME.html file.");
        }
        return htmlFile;
    }

使用浏览器打开html文件:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(htmlFile), "text/html");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);