using this code
WebView wvChangelog = (WebView) alertDialog.findViewById(R.id.wv_changelog);
wvChangelog.loadUrl("file:///android_res/raw/changelog");
I am able to load and show a html file from the res/raw folder, what I need to do is to load files from respected locale folders eg raw/changelog , raw-de/changelog, raw-fa/chenglog I believe the same snippet should work, but it only loads the main file (located in raw folder). although other locale related stuff in the same context (eg the title string loaded by getString(R.string.title) is respecting the locale.
Am I missing something here?
also I'm aware of InputStream inputStream = getResources().openRawResource(R.raw.filename); but as you can see the WebView does not accept Input Stream and I have to comeup with a file:///android_res... type
I solved this problem by creating multiple raw folders. Then to load each one specifically according to the language , That's the phone's language has to be changed, Settings >> Language and Input. Then it will choose the files accordingly.
Related
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?
I have an Android app that displays a comic book. To make use of the built-in zoom controls, I am loading the pictures in a WebView like so:
webView.loadUrl("file:///android_asset/page1.jpg");
This is working just fine, however, since the images are in the assets folder, they are not being compressed which makes my .apk enormous. I was wondering how to reference resource files (from the res/drawable folder) with a file path like I did above with the assets. Does anyone know what that path would look like? I've tried things like "file:///res/drawable/pagetitle.jpg" with no success. Thanks for the help.
Update:
I found that "file:///android_res/drawable/page1.jpg" was the path that I was looking for.
from this site
Using the resource id, the format is:
"android.resource://[package]/[res id]"
Uri path = Uri.parse("android.resource://com.androidbook.samplevideo/" + R.raw.myvideo);
or, using the resource subdirectory (type) and resource name (filename without extension), the format is:
"android.resource://[package]/[res type]/[res name]"
Uri path = Uri.parse("android.resource://com.androidbook.samplevideo/raw/myvideo");
I also had to use loadDataWithBaseURL instead of just plain loadData to get the file:///android_res/drawable/page1.jpg to work.
I am using webview in android app, the webview will load a html page contains file upload control.I override the "openFileChooser" function, so I can select file in webview,but after I choose file and return to webview, the file name while change to url-encoded format(If the file name isn't ascii character) ?
There's nothing you can (or should) do here.
The operating system handles these things, i.e. placing the selected file in the value attribute of the <input type="file">.
But there's nothing wrong with these URL-encoded names. You shouldn't rely on the filenames from the client side, anyway.
What you should use is only the file's content and maybe its size and extension as reported.
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);
I have few html files in assets folder of my application. My application loads these files depending on the device language. When I check for the existance of the file it say does not exist, but when I load that file using browser.loadUrl(filename), it loads it fine.
Following code will help you to understand my problem:
String filename="file:///android_asset/actualfilemname.html";
File f = new File(filename);
if(!f.exist){
filename = "file:///android_asset/newfile.html";[Everytime it loads this file even though I have actualfilename.html in the folder]
}
browser.loadUrl(filename);
[it loads the newfile.html but not actualfilename.html]
You can't use File for resources. You'll need to use the AssetManager for that.
(In the off-chance that File does handle resources, which I don't think it does, you'll have to convert the path to a URI first, for example using URI.create(). File(String) expects a path, not a URI.)
Is this the exact code you are using? you probably want to be calling f.exists() not filename.exist().
Edit: try working with the AssetManager instead of hard coding your file path. My best guess is that the file path you are using is not exactly how it supposed to be.