kishidp
kishidp

Reputation: 1948

How to Open an xhtml file in Android webview

I'm developing an epub reader. I was able to extract the files from the epub and create a filetree in the device sdcard. However, when I try to read the .xhtml file. It is not working. I tried opening an html file instead and it works.

Here's my code for loading the .xhtml

webView = (WebView) findViewById(R.id.webview);
webView.setWebViewClient(new ReaderWebClient());
webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDisplayZoomControls(false);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAllowFileAccess(true);
webView.loadUrl("file:///" + Environment.getExternalStorageDirectory() + "/epub/EPUB/cover.xhtml");

I just used a hardcoded for the url first as to try if it can load the page.

The webview just displays, "Webpage not available"

Am I doing something wrong? Thanks for any help

Upvotes: 1

Views: 2596

Answers (4)

Uday Sravan K
Uday Sravan K

Reputation: 1454

This is my working code .Try it.

        if(android.os.Build.VERSION.SDK_INT>=android.os.Build.VERSION_CODES.HONEYCOMB)
        {
            getSettings().setAllowContentAccess(true);
        }
        getSettings().setJavaScriptEnabled(true);
        getSettings().setLayoutAlgorithm(LayoutAlgorithm.NORMAL);
        getSettings().setAllowFileAccess(true);     
        if(android.os.Build.VERSION.SDK_INT>=android.os.Build.VERSION_CODES.JELLY_BEAN)
        {
            getSettings().setAllowUniversalAccessFromFileURLs(true);
        }   
        loadUrl("file://"+Environment.getExternalStorageDirectory()+"/epub/EPUB/cover.xhtml");

once cross check your file path.

Upvotes: 0

kishidp
kishidp

Reputation: 1948

My solution for this is I get the data for each item (.xhtml file) and pass it as an argument to loadDataWithBaseUrl(). I just have to determine the correct path for the files so that the images and CSS can be loaded correctly.

String baseUrl = "file:///" + Environment.getExternalStorageDirectory() + "/epub" + bookFilename + "/OPS/";
String data = new String(bookReader.book.getContents().get(2).getData());

webView.loadDataWithBaseURL(baseUrl, data, "application/xhtml+xml", "utf-8", null);

Upvotes: 2

harish525
harish525

Reputation: 3

Use this:

webView.loadUrl("file://" + Environment.getExternalStorageDirectory().getAbsolutePath() + "epub/EPUB/cover.xhtml");

and take permission for writing to storage in manifest file, like this:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Upvotes: 1

user3540792
user3540792

Reputation: 56

Try this:

webView.loadUrl("file://" + Environment.getExternalStorageDirectory().getAbsolutePath() + "epub/EPUB/cover.xhtml");

Upvotes: 0

Related Questions