6

我正在制作一个应用程序,我想在浏览器中打开一个本地 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());   

    }
4

5 回答 5

4

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

  1. 将 html 文件复制到共享存储,以便网络浏览器可以访问该文件。
  2. 然后在应用程序的新 Activity 或片段中实现 WebView webview.loadUrl("file:///android_asset/www/trialhtml.html");

您不需要像其他答案指导您那样阅读资产。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 浏览器,都会选择用户默认值。

于 2013-02-08T13:17:21.687 回答
1

打开并加载为原始资源,然后放入WebViewloadData

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

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

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

于 2013-02-08T13:18:22.487 回答
0

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

AssetManager am = context.getAssets();
InputStream is = am.open("trialhtml.html");
于 2013-02-08T13:15:32.393 回答
0

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

webView.getSettings().setJavaScriptEnabled(true);

进入您的活动,并将 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");
于 2013-02-08T14:12:30.307 回答
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);
于 2015-07-30T13:35:45.240 回答