1

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I have tried with this: Intent i = new Intent(Intent.ACTION_VIEW); i.setData(Uri.parse("file:///assets/iCD_credits_it.html")); i.setClassName("com.android.browser", "com.android.browser.BrowserActivity"); me.startActivity(i);
@sefiroths: That is not one of the solutions I outlined in my answer, and you probably found that it did not work.
in fact it didnt work...I thought that could be only because it was the wrong path...however how can i add a webview without adding xml on a top of a cocos2dx layer? adding a button only to test not work: LinearLayout layout = new LinearLayout(me); layout.setOrientation(LinearLayout.VERTICAL); //Can also be done in xml by android:orientation="vertical" ...
Button btnTag = new Button(me); btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); btnTag.setText("Button"); btnTag.setId(1000); layout.addView(btnTag); me.setContentView(layout); where me=this in onCreate
@sefiroths: I have never used cocos2dx and so I have no idea how one would integrate a WebView with it, sorry

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.