I am using xamarin forms and for android I would like to fetch local web content (not store it in android assets) so that it can be updated independently of the app itself.
What folder should be used as I know that WebView is running as separate process so not all folders are accessible?
Is there any doc describing what folders WebView can reach?
An easy way is to add the html in raw folder for the Resource folder. Android provideds Resources.OpenRawResource to read the stream from the file.
Create a raw folder inside the Resource folder. And add the local html file in it.
The code sample below for your reference:
SetContentView(Resource.Layout.layout4);
var stream = Resources.OpenRawResource(Resource.Raw.local);//local is the html file.(local.html)
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
var text = new System.Text.StringBuilder();
string line;
while ((line = reader.ReadLine()) != null)
{
text.AppendLine(line);
}
var webView1 = FindViewById<WebView>(Resource.Id.webView1);
webView1.LoadData(text.ToString(), "text/html", "UTF-8");
Related
I'm new to Xamarin Forms and would like to include some read-only data in a json file to be deployed with the package that will be used as a local data store. How do you add this resource to the respective project types? ie iOS and Android.
You can put the json file in the Froms file directory, then click the file->Properties->Build Action to select Embedded resource.
You can get it using the code below:
var resourcePrefix = "MyApp.";//your project name.
var assembly = IntrospectionExtensions.GetTypeInfo(typeof(MainPage)).Assembly;
Stream stream = assembly.GetManifestResourceStream(resourcePrefix + "aaa.json");//your json file name
using(var streamReader = new StreamReader(stream))
{
var reader = streamReader.ReadToEnd();
}
My android app opens up a ZIM File stored on device. The ZIM File has multiple HTML pages in it. I want to read the text from a single HTML page. The HTML page cannot be accessed directly with it's filePath but the ZIM file could be.
For example:
/storage/emulated/0/Download/wikipedia_en_ray_charles_2018-10.zim
Since there's no filePath for the HTML page to open it directly. The only way to read text from it as per my knowledge would be while it's opened in the app. So, my question is that if there is any way I could read text from a HTML page while it's already opened in the app and there's no way to access it(AFAIK).
The only thing I'm getting for the HTML page is it's name combined with the content uri.
content://org.kiwix.kiwixmobile.zim.base/Ray_Charles.html
But this couldn't be accessed from the device.
Here's the code I've written that works for HTML files on device which have a file path.
`
String answer = "";
try {
FileReader index = new FileReader(selectedFilePath);
BufferedReader reader = new BufferedReader(index);
String line = "";
while ((line = reader.readLine()) != null) {
answer += line;
}
reader.close();
`
I am working on an application which supports 2 languages English and German. I'm loading some html files from raw folder to display it in webview. For that I've created 2 raw folders namely "raw" and "raw-de". It properly loads html from respective raw folders when language is changed on Pre-Oreo devices but it's not working on Oreo. Below is the code to load html file from raw folder.
txt_desc.loadUrl("file:///android_res/raw/what_is_ergo.html");
I'm not able to figure out how to make it work on Android 8.0. Any help would be appreciated.
You can try reading raw file like this:
byte[] buffer = new byte[1024];
StringBuilder builder = new StringBuilder();
val inputStream = context.getResources().openRawResource(R.raw.what_is_ergo);
while (it.read(buffer) != -1) {
builder.append(String(buffer));
}
txt_desc.loadDataWithBaseURL("", builder.toString(), "text/html", "UTF-8", "");
I am trying to create a local web server using NanoHTTPD on Android. I know how to use it for a single file doing something like:
#Override
public Response serve(IHTTPSession session) {
String answer = "";
try {
FileReader index = new FileReader(fileLocation);
BufferedReader reader = new BufferedReader(index);
String line = "";
while ((line = reader.readLine()) != null) {
answer += line;
}
reader.close();
} catch(IOException ioe) {
Log.w("Httpd", ioe.toString());
}
return newFixedLengthResponse(answer);
}
and setting it to port 8080 so I can browse to http://localhost:8080 and view the file. However, I would like to use NanoHTTPD to put an entire directory on the server and not just a single file, in order to run an HTML5 game. Kinda similarly to how I can browse to a folder in Command Prompt on Windows and use
python -m http.server 4444
in order to browse through the folder as if it were a website at http://localhost:4444. I'm not sure how to go about it using NanoHTTPD. Can someone help me figure out where to start, or is there another library out there that's much easier to use?
Thanks in advance. Any help would be greatly appreciated.
You are on the right path.
Quick answer look here.
What you shod do is generate HTML files of directory structure to the user.
Meaning when the user is first contacting you read the local directory that you determine as root directory and create an HTML file as a response with the file names as links.
If the user then clicks on a single file serve it like you already do.
I found an answer demonstrating how to generate HTML files dynamically that you can use here.
When creating an Android application, I have some files that needs to be stored on the android itself.
How do I do this?
If you have local files, like some error tones, some openning video.. then place it in you assets folder of your project.
If you have dynamic data need to download at run time then use this guide.
Best place for generic files would be the assets folder.
You can access files through the AssetManager, which you can get with Activity.getAssets() for example.
Here is an example how you could access a text file:
try {
BufferedReader br = new BufferedReader(
new InputStreamReader(getAssets().open("sometextfile.txt")));
// do stuff with br
} catch (IOException e) {
e.printStackTrace();
}
For more information on AssetManager read the Java Doc. Oh and yes, you can create folders in assets.
If you want to keep some files like readme.txt or even music files, you can use the raw folder inside of res folder. So create a folder named raw inside of res folder.
Inside of raw folder, let us assume that there is a file named readme.txt, assuming that the Activity class is called MyActivity.
Now, you can read the contents of a file into a String as shown below:
StringBuilder strContents = new StringBuilder();
String thisLine;
InputStream is = MyActivity.this.getResources().openRawResource(R.raw.readme);
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
while ((thisLine = br.readLine()) != null) { // while loop begins here
strContents.append(thisLine);
}
br.close();
//Now you have the data in strContents
Alternately, assets is also one such folder that you can use since the raw folder contains the file as is without any optimization, zipping done by Android.
So create an assets folder in your Project root folder and place your files there e.g. myfile.
Now, you can get an instance of the file InputStream as given below:
InputStream is = getBaseContext().getAssets().open("mydb");