sefiroths
sefiroths

Reputation: 1563

Android load local html file and display in web browser

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

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

Upvotes: 1

Views: 1848

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006539

There are several problems here.

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

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.

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.

Either:

  • Use a WebView and display it yourself, or

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

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

  • 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.

Upvotes: 1

Related Questions