Android的加载本地HTML文件,然后显示在Web浏览器 [英] Android load local html file and display in web browser

查看:870
本文介绍了Android的加载本地HTML文件,然后显示在Web浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这code加载网页不在本地工作正常,但当我把本地文件有一个错误:未找到文件。
我使用cocos2dx和file.java包含活动我有一个功能,使:

This code loading a webpage not local works fine but when i put a local file there is an error: file not found. I'm using cocos2dx and in file.java that contains Activity i have a function that makes:

File file=new File("/android_asset/iCD_credits_it.html");
Uri uri = Uri.fromFile(file);
Intent browserIntent = new Intent(Intent.ACTION_VIEW);
browserIntent.setDataAndType(uri, "text/html");
browserIntent.setClassName("com.android.browser", "com.android.browser.BrowserActivity");
//browserIntent.setData(uri);
me.startActivity(browserIntent);

我已经把iCD_credits_it在资源文件夹中,我可以看到它在Eclipse的资产文件夹
我怎样才能显示浏览器的网页?
谢谢

I have placed iCD_credits_it in Resource folder, and I can see it in assets folder of eclipse How can I display that webpage in browser? Thanks

推荐答案

有这里有几个问题。

首先,有一个 iCD_credits_it.html 没有 / android_asset 目录中的设备的根,更不用说文件中了。

First, there is no /android_asset directory in the root of the device, let alone an iCD_credits_it.html file in it.

二,你的 setClassName()将导致此code崩溃在许多设备上。 不要硬code包和类名,除非你只打算在自己的设备上运行你的应用程序。

Second, your setClassName() will cause this code to crash on many devices. Do not hard-code package and class names, unless you only plan on running your app on your own device.

三,无论是文件,在资源文件夹,或者是资产的文件夹,或者您有该文件的两个副本。资源和资产,而类似,都是不一样的东西。

Third, either the file is "in Resource folder", or it is "in assets folder", or you have two copies of the file. Resources and assets, while similar, are not the same thing.

第四,你将有很大的困难能够从资源或项目的资产读取文件的浏览器。资产是根本不可能的;发现可以读取HTML出您的原始资源的浏览器仅仅是不可能的。

Fourth, you will have great difficulty finding a browser capable of reading files from either resources or assets of your project. Assets is simply impossible; finding a browser that could read HTML out of your raw resources is merely improbable.

或者:


  • 使用的WebView 和自己显示出来,或

自己写的的ContentProvider 来为你的HTML出资产/项目,或

Write your own ContentProvider to serve your HTML out of assets/ of your project, or

尝试我的 StreamProvider 一个罐头的ContentProvider 可充当文件从资产/项目的,或者

Try my StreamProvider for a "canned" ContentProvider that can serve files out of assets/ of your project, or

将文件复制到外部存储(例如, getExternalFilesDir()),并试图让浏览器从那里加载它

Copy the file to external storage (e.g., getExternalFilesDir()) and try to get a browser to load it from there

只有第一个是保证工作。其他人会根据浏览器在用户的处置能力。

Only the first one is guaranteed to work. The others will vary based on the capabilities of the browsers at the user's disposal.

这篇关于Android的加载本地HTML文件,然后显示在Web浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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