Loading html file from local folder into webview - android

I'm new to Android development.
I want to load a html file into a webview.
Note that there are so many relevant questions on SO like this, but they all deal with getting **.html* from assets folder.
But I want to load html file from local folder, say "D://abc.html" because if my html is around 10Mb then corresponding apk size also goes upto 10mb.
Any help appreciated.
EDIT
I tried webView.loadUrl("file:///D:/Projects/myWebsite/index.html");
but it gives Web page not available and File not found error.

You can use:
WebView webView = // ...
webView.loadUrl("file:///myPath/myFile.html");
In an Android application, files can be read from 3 types of locations:
Internal storage: Each app has its own, file names are relative to this location. URL takes form file:///myFolder/myFile.html
External storage: Needs permission and may not always be available. Get the root folder by calling Environment.getExternalStorageDirectory(). So, construct the URL using: String url = "file:///" + Environment.getExternalStorageDirectory().toString() + File.separator + "myFolder/myFile.html"
Assets: Stored in the apk. Read-only access. URL takes form file:///android_asset/myFolder/myFile.html (See also Loading an Android Resource into a WebView)

In Android 4.4 KitKat, a "Not allowed to load local resource: file:///.." is thrown.
arise when loadURL and the only alternative I've found is "loadDataWithBaseURL".
webView.loadDataWithBaseURL("file:///android_asset/demo/",
tmpDocumentText,"text/html", "UTF-8", null);

WebView has loadData method http://developer.android.com/reference/android/webkit/WebView.html
All you need to do is reading the file into String then feed it to WebView using loadData.

either one can use this way to load file specifically if the file is present within android studio project
(pls note android_res is res folder actually and should not be replaced)
webView.loadUrl("file:///android_res/drawable/FILENAME.EXTENSION");
but if that is not working please try another way of loading file like
webView.loadDataWithBaseURL("file:///android_asset/demo/",tmpDocumentText,
"text/html", "UTF-8", null);

Related

How can I use an Html file in a raw Android folder to populate a Xamarin Forms webview?

I have an html file saved on an Android device here:
/data/data/MyApp.Mobile.Forms/files/4650-0/11456.pptx.html
I'm able to use FileReaders to look at the Html. However when I set it as my webViews HTML source, like so:
var path = Directory.GetFiles(directoryPath, "*.pptx.html")[0];
string htmlString = File.ReadAllText(path);
var htmlWebViewSource = new HtmlWebViewSource();
htmlWebViewSource.Html = #htmlString;
htmlWebViewSource.BaseUrl = DependencyService.Get<IBaseUrl>().Get();
webView.Source = htmlWebViewSource;
I get an activity indicator but no webview loads. I've tested other sites, e.g. Google, and raw html, and these work. But for some reason, my html does not.
I've also seen other approaches using stream readers, but none of these have worked for me.
I see a lot of talk of my html file needs to be in the assets folder. But the file is saved at runtime and files can't be saved to assets at runtime.
Please help me work out what I'm missing here. Can I have a webview using a file from that data/data path?

Opening an external file through a WebView in Android Studio

So I have been at it for a while, and I really cannot get it to work. What I want is this:
To access some file outside the project folder through a WebView.
Here is some code that I have been trying to get it working:
String url = "file://///\\myProjectName" + Environment.getExternalStorageDirectory().toString()+ File.separator + "hw.html";
WebView webView = (WebView) findViewById(R.id.WebviewLOAD);
webView.loadUrl(url);
I just get a ERR_FILE_NOT_FOUND with any type of file. For a test-HTML file I created, I get this along with file not found error:
"The webpage at file:///myProjectName/storage/emulated/0/hw.html could not be loaded because"....
I tried removing Environment.getExternalStorageDirectory().toString() but then i just get file:///myProjectName/hw.html along with the file not found error.
So I want to navigate to the right file and be able to open it, but I just can't seem to get it right. I'm open for any suggestions?
So, basically you can't do this. Instead you want to run a local server in the background and make the WebView and server work together.
I did it with Jooby with an SQLite database.

Load an external html file into webview

I'm having problems loading an external html file into the webview. I've done this before and it should be easy, but for some reason I keep getting Web page not available.
I know the files are in the directory because I placed them myself using file explorer.
String filename = "file:///"+ Environment.getExternalStorageDirectory() + File.separator + "Android/data/com.example/files/test_html2.html";
webview.loadUrl(filename);
I've tried moving the files into root and trying there, I've removed file:// and replaced it with content:// and nothing at all. I have read permissions in the manifest.
Any ideas?
Don't create file:// URLs yourself, as you will tend to screw them up. In this case, I think that you have four slashes after the :, three that you typed in and one from Environment.getExternalStorageDirectory().
Instead, create a File object and use that as the basis:
File f = new File(Environment.getExternalStorageDirectory(), "Android/data/com.example/files/test_html2.html");
webview.loadUrl(f.toURI().toURL()); // or use Uri.fromFile(f).toString() instead

using files stored in the SDCard as resourses for internal website on a Browser

I need to download some resources from the net, then locally process them and show them on a BrowserActivity.
Since the resources (images) could be really big I'm saving them in the SDCard.
How may I refer to those stored resources from an html page? The html page is embedded into my app (It's in assets folder). I think this will fire some kind of security issues.
Is this the right approach? or is there any better solution?.
Thanks
Answer for question 1): You can refer to file on your sd card using "file://" prefix. See example below:
String fileName = "x"; // here is the file name of the downloaded image
String imagePath = "file://" + getApplicationContext().getFileStreamPath(fileName.getAbsolutePath();
String html = html+"<img src=\"" + imagePath + "\"/>" // embed that code in your modified html source.

load somefile.html file from internal storage to a webview of an app

I have a file in the internal storage of my tablet. /myfolder/subfolder/index.html. How can i load this into a webview of an app.
i have tried
webview.loadURL("file:///myfolder/subfolder/index.html");
but it is not giving the expected result. it says web page not available.
I know how to load from the asset folder or from the web/internet. but i need to load local file into webview. is it possible.?
File file = new File("/data/data/packagename/foldername/");
webView.loadUrl("file:///" + file);
I was able to solve my problem by using the following as path:
webview.loadURL("file:///mnt/sdcard/myfolder/subfolder/index.html");
An app cannot access data from the Internal storage stored by another app. Permissions are applied to internal storage that make data written by an application not accessible outside of that application (your app cannot read anything written by another app).
So, if you are accessing a file that is not created by your app, AFAIK, you cannot have access to it.
BTW, you could access the file from the internal storage as below,
webview.loadURL("file:///data/data/com.yourproject.example/files/index.html");
Put your html files in asset folder access the page like given below.
webview.loadURL("file:///"+mContext.getFilesDir()+"/myfolder/subfolder/index.html");
you have to mention the android asset while accessing html pages in android assets.
File gameDir = new File("/data/data/" + getActivity().getPackageName() + "/games");
gameDir.mkdirs();
// Create and execute the background task.
mTask = new DownloadTask();
mTask.execute("https://github.com/gabrielecirulli/2048/archive/master.zip", "/data/data/" + getActivity().getPackageName() + "/games/2048.zip");
mWebView.getSettings().setJavaScriptEnabled(true);
Toast.makeText(MainActivity.this, path+"/index.html", Toast.LENGTH_SHORT).show();
mWebView.loadUrl("file:///data/data/com.example.zipfiledownload/games/2048-master/index.html");

Categories

Resources