3

I am trying to access the url using the shouldOverrideUrlLoading method. For some reason when I try to access the url that are present in the html files, I get Web page not available error.Previously I could access the url because I stored my html file in the raw folder but I needed to move it to assets folder as the code looks cleaner this way. This is my code, could anyone let me know how I can resolve this.

webview.setWebChromeClient(new WebChromeClient());
webview.setWebViewClient(new WebViewClient());
webview.loadUrl("file:///android_asset/myfile/file.html");
webview.setVerticalScrollBarEnabled(false);
webview.setWebViewClient(new WebViewClient()
{
    public boolean shouldOverrideUrlLoading(WebView view, String url) {

        if (url.equalsIgnoreCase("some text")){
            setDialog("some fancy text");
        }
Haresh Chaudhary
  • 4,390
  • 1
  • 34
  • 57
user788511
  • 1,726
  • 2
  • 30
  • 52
  • 2
    why are you calling setWebViewClient() twice? also is your html file in a subfolder or directly in the assets folder? – FoamyGuy Jul 24 '12 at 17:53
  • Thanks Tim, I honestly missed that, however yeah the file is in a subfolder in the assets folder.. – user788511 Jul 24 '12 at 17:56
  • what does it show instead of of the web page? is there any message in the logcat when loadUrl call happens? – FoamyGuy Jul 24 '12 at 17:57
  • Sorry Tim but there is no message in the logcat, when I try to access the url, a web page not available page is displayed with the error..web page at file:///android_asset/File/sometext could not be loaded as the requested file was not found.. – user788511 Jul 24 '12 at 18:07
  • try moving it out of the subfolder, so it will be directly in the assets folder. I don't know why that would help but its worth a shot. – FoamyGuy Jul 24 '12 at 18:09
  • Okay Tim, let me try that and let you know..thanks – user788511 Jul 24 '12 at 18:09
  • Sorry Tim, still not working :( – user788511 Jul 24 '12 at 18:12
  • 2
    `file:///` is considered "insecure" as of Android 4.0. Chances are your code will actually work on a device running Android 2.3 or lower... however, it apparently no longer works as of Ice Cream Sandwich. I was never able to figure out a working alternative :/ – Alex Lockwood Jul 30 '12 at 04:49
  • Have you ever tried to `loadUrl` after `setWebViewClient`? It's a good practice usually! Let me now about it... – yugidroid Aug 05 '12 at 22:40
  • You need to loadUrl last. Also "some text" has to match your url. – sww314 Aug 06 '12 at 05:42
  • yugidroid, I do add the loadUrl after setWebViewClient but it still does not work :( Help me please – user788511 Aug 07 '12 at 17:30

8 Answers8

3

Try to get the Real Path of the file first and then try as your way like this:


String YourURI="/android_asset/myfile/file.html";
YourURI = new File(getRealPathFromURI(YourURI));

//Got the Real Path Of the File

private String getRealPathFromURI(Uri contentURI) {
  Cursor cursor = getContentResolver().query(contentURI, null, null, null, null); 
  cursor.moveToFirst(); 
  int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); 
  return cursor.getString(idx); 
}

webview.setWebChromeClient(new WebChromeClient());
webview.setWebViewClient(new WebViewClient());
webview.loadUrl(YourURI);                      //**Used the Real Path**
webview.setVerticalScrollBarEnabled(false);
webview.setWebViewClient(new WebViewClient()
{
    public boolean shouldOverrideUrlLoading(WebView view, String url) {

        if (url.equalsIgnoreCase("some text")){
            setDialog("some fancy text");
        }
}    

If the File is in the SDCARD :-


webview.setWebChromeClient(new WebChromeClient());
webview.setWebViewClient(new WebViewClient());
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
  Log.d(TAG, "No SDCARD");
} 
else 
{
  webview.loadUrl("file://"+Environment.getExternalStorageDirectory()
                                   +"/android_asset/myfile/file.html");
}
webview.setVerticalScrollBarEnabled(false);
webview.setWebViewClient(new WebViewClient()
{
    public boolean shouldOverrideUrlLoading(WebView view, String url) {

        if (url.equalsIgnoreCase("some text")){
            setDialog("some fancy text");
        }
}    

Final Edit:- This Code Works Perfectly for Asset Folder


public class MyActivity extends Activity {  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview);  
        WebView webview;  
        webview = (WebView) findViewById(R.id.webView1);  
        webview.loadUrl("file:///android_asset/myfile/file.html");
    }  
}
Haresh Chaudhary
  • 4,390
  • 1
  • 34
  • 57
  • Haresh, could you provide a solid example for me please? – user788511 Aug 02 '12 at 18:09
  • Haresh I get File can not be resolved to a type error, I cant progress,please advise – user788511 Aug 06 '12 at 17:38
  • Is the file located in the SDCARD?? If so then try the latest edit of my Answer – Haresh Chaudhary Aug 07 '12 at 04:35
  • No Haresh, it is stored in the assets folder as I showed in the code, I just need to find a way of parsing the contents in the android assets folder and then use the loadUrl method..any ideas? Thanks for all the help – user788511 Aug 07 '12 at 04:47
  • Please Check my Final Edit.You can try this creating a new Project for trail. – Haresh Chaudhary Aug 07 '12 at 04:56
  • Haresh, I am sorry for not being clear, I can access the files in the assets folder but can not access the url links in those files so I can not use shouldOverrideUrlLoading method..this is where I am stuck.. – user788511 Aug 07 '12 at 05:05
1

There is another way that would also work for you---

webView.loadData(customHtml, "text/html", "UTF-8");

where custromHtml is the HTML line that is used to be displayed.

on the place of "custromHtml" you just pass "file://android_asset/myfile/file.html".

I think this should work.

Kumar Shorav
  • 531
  • 4
  • 16
1

You may try for this too -

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        String myURL = "file:///android_asset/index.html";
        myBrowser=(WebView)findViewById(R.id.mybrowser);

        /*By default Javascript is turned off,
         * it can be enabled by this line.
         */
        myBrowser.getSettings().setJavaScriptEnabled(true);
        myBrowser.setWebViewClient(new WebViewClient());

        myBrowser.loadUrl(myURL);

    }

and also check you have placed the file in correct folder of main project. If all this is ok, make a clean project and fix project. Then try to run it.

Kumar Shorav
  • 531
  • 4
  • 16
  • Thanks Kumar, I have followed your advice but I can not get shouldOverrideUrlLoading to point to the dialog I want to show..this is so frustrating, it can not access the html file in the assets folder – user788511 Aug 02 '12 at 18:08
0
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        String myURL = "file:///android_asset/index.html";
        myBrowser=(WebView)findViewById(R.id.mybrowser);
        myBrowser.getSettings().setJavaScriptEnabled(true);
        myBrowser.setWebViewClient(new WebViewClient());

        myBrowser.loadUrl(myURL);

    }
iAndroid
  • 951
  • 7
  • 18
0

This would work well try it-----

"android.resource://[package]/[res type]/[res name]"

Uri path = Uri.parse("android.resource://com.androidbook.samplevideo/raw/myvideo");

This link is also useful -

http://developer.android.com/reference/android/webkit/WebView.html

Loading an Android Resource into a WebView

Community
  • 1
  • 1
Kumar Shorav
  • 531
  • 4
  • 16
0

shouldOverrideUrlLoading does not work on ICS for file://, but works for fine for external urls. loadURL works fine loading from the assets folder.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    WebView web=(WebView)findViewById(R.id.webView1);

    web.setWebViewClient(new MyWebClient());
    //web.loadUrl("file:///android_asset/index.html");
    web.loadUrl("http://google.com");

.....

class MyWebClient extends WebViewClient
{
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        Log.d("MyWebClient", "url is: " + url);

        if (url.equalsIgnoreCase("google.com")){
            Log.d("MyWebClient","url is: " + url);
        }
        return false;
    }

}
sww314
  • 1,252
  • 13
  • 17
0

I found this forum solution which solved the problem for me: http://www.cynosurex.com/Forums/DisplayComments.php?file=Java/Android/Android_4_has_a_.file....android_asset.webkit.._bug

A. Masson
  • 2,287
  • 3
  • 30
  • 36
-1

I think you should try this ....

webview.setWebChromeClient(new WebChromeClient());
webview.setWebViewClient(new WebViewClient());
webview.loadUrl("file://android_asset/myfile/file.html");
webview.setVerticalScrollBarEnabled(false);
webview.setWebViewClient(new WebViewClient()
{
    public boolean shouldOverrideUrlLoading(WebView view, String url) {

        if (url.equalsIgnoreCase("some text")){
            setDialog("some fancy text");
        }
Kumar Shorav
  • 531
  • 4
  • 16