WebView not loading local HTML in Android? - android

I am trying to load a local html file in a WebView but it always give error
new::ERR_FILE_NOT_FOUND
I have attached the images of code I am using and the file directory. I have tried it with different directories but it didn't work.
Changed the file directory to res folder, res/raw folder.
tried to load different html
it does load the webvlinks
Path and the code image is here

It is good to place all file assets files in the assets folder.
So Add your HTML file to the assets folder and load webview with below code.
webview.loadUrl("file:///android_asset/privacy_policy.html");
Or if you don't want to move the file from that "sampledata" folder, then use below code.
webview.loadUrl("file:///sampledata/privacy_policy.html");

I had the same problem, I found this answer here: https://inneka.com/programming/android/loading-html-file-to-webview-on-android-from-assets-folder-using-android-studio-2/
try {
AssetManager assetManager = this.getAssets();
InputStream stream = assetManager.open("editor.html");
BufferedReader r = new BufferedReader(new InputStreamReader(stream));
StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
total.append(line).append("\n");
}
webView.loadDataWithBaseURL(null, total.toString(), "text/html", "UTF-8", null);
} catch (Exception xxx) {

Related

Reading from a file in android studio and testing on phone

I am trying to read from a text file in Android Studio.
FileInputStream fin=new FileInputStream("./quizdata.txt");
int size = fin.available();
byte[] buffer = new byte[size];
fin.read(buffer);
fin.close();
Now, when I run this test on a phone connected to my computer, android doesn't find this file. My best guess is, the file should be on my phone and not on the computer.
If yes, at what location in the phone shall I store this file?
Thanks
You need to add the file to your Android code, not to your Android phone. You need to save it either in assets folder (src/main/assets) or raw folder (src/res/raw).
If you save it in the raw folder, you can read it with:
// you don't need to have the file extension. Note that you can't use uppercase
InputStream is = Context.getResources().openRawResource(R.raw.quizdata);
If you save it in asset folder, you can read it with:
AssetManager am = context.getAssets();
InputStream is = am.open("quizdata.txt");
To get all the lines in the file, you can use the following code:
// getting all the line
BufferedReader r = new BufferedReader(new InputStreamReader(is));
StringBuilder quiz = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
quiz.append(line).append('\n');
}

FileReader file path in Android

I created A Buffered Reader, File Reader in my Android app and placed two text files inside raw folder. Now I have to pass a file path into File Reader, but what is the path of my file now?
BufferedReader reader = new BufferedReader(new FileReader("path"));
image link
but what is the path of my file now?
You don't have to pass file path into FileReader for reading file, here you can check following code snippet.
InputStream inputStream = null;
try {
inputStream = getResources().openRawResource(R.raw.hello_world);
byte[] reader = new byte[inputStream.available()];
while (inputStream.read(reader) != -1) {}
editField.setText(new String(reader));
editField.setSelection(editField.getText().length());
} catch(IOException e) {
Log.e(LOG_APP_TAG, e.getMessage());
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
Log.e(LOG_APP_TAG, e.getMessage());
}
}
}
}
Raw folder is for what in ANDROID
Arbitrary files to save in their raw form. To open these resources with a raw InputStream, call Resources.openRawResource() with the resource ID, which is R.raw.filename.In above example R.raw.hello_world.
However, if you need access to original file names and file hierarchy, you might consider saving some resources in the assets/ directory (instead of res/raw/). Files in assets/ are not given a resource ID, so you can read them only using AssetManager.
Raw mostly used with media files.
By default, you will be working in your present class path. So, you have to go back to the folder where the raw files is present and go inside it and access the files you want to.
file:///
will go back to your project directory, the one that contains all the folders. From there , you can access the raw folder.

Read textfile in Android - where to place the file

Where do I place a certain textfile in the project of Eclipse if I want to read the contents?
Which objects should I use?
I tried with the following syntax but the file could not be found? Maybe the computers filesystem is used in this case?
private void read() throws FileNotFoundException, IOException {
File file = new File("test.txt");
FileReader fileReader = new FileReader(file);
BufferedReader buffReader = new BufferedReader(fileReader);
String[] stringBuffer = new String[2];
String line;
int i = 0;
while ( (line = buffReader.readLine()) != null) {
stringBuffer[i] = line;
i++;
}
System.out.println(stringBuffer[0] + stringBuffer[1]);
}
My idea was that the search begins in the rootfolder of the project but I guess its completely wrong because the computers filesystem is used?
If you want to bundle this file with your Android application at build time, you have two choices: the /raw folder under /res or the /assets folder. You should place your file here and access it in the correct way for the chosen directory. For more information, read more about the built in folders for an Android project here.
For an example, if you store your text file in /assets/, which is most likely the correct place for a text file, you need to access the contents of the file using AssetManager. Let's assume you created a file called:
/assets/test.txt
You can access the file as such, assuming that you are doing this from an Activity, so that the this keyword points to your Activity:
AssetManager assetManager = this.getResources().getAssets();
InputStream input = assetManager.open("test.txt");
You can now use that input stream to read from the file in a way similar to how you have done in your original question. You can use that InputStream to create a BufferedReader, as below, assuming your text content is encoded in UTF-8:
BufferedReader br =
new BufferedReader(new InputStreamReader(input, "UTF-8"));

How to change decoration of a local HTML file to show it on the WebView in Android

In my android application I used to open a local HTML file like the one below:
mPath = ... myHTML.html
webView.loadUrl("file:///" + mPath);
Now I want to make some changes in my HTML file such as font size, align from left to right and right to left and ... I guess that I can do that by adding a CSS file to head of my HTML, but the problem is I have no idea how to access the source of my HTML file and convert it to string.
Is there any better way to change the decoration of a local HTML file? if not, how can I access the source of my HTML file?
OK I'm answering my own question. I used Panos's answer on: https://stackoverflow.com/a/13357785/1572408
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
return sb.toString();
}
In onCreate():
File fl = new File(myLesson);
FileInputStream fin;
fin = new FileInputStream(fl);
String ret = convertStreamToString(fin);
ret = ret.replace("<body>","<body align='right' style='font-size:14pt;>");
webView.loadDataWithBaseURL("file:///android_asset/", ret, "text/html",
"UTF-8", "");

How to show "®" read from text file in assets directory?

Does anybody know the best way to show the "®" symbol when read from a text file in the assets directory? I've tried replacing the "®" symbol with &® or ® or \u00AE, but none of these work.
Is the only solution to define it as html?
Here's the code I'm using to read the text file from assets:
mTextViewTermsConditions = (TextView) findViewById(R.id.textView_terms_conditions);
StringBuffer stringBufferEula = new StringBuffer();
try
{
InputStream inputStream = getAssets().open("eula");
BufferedReader f = new BufferedReader(new InputStreamReader(inputStream));
String line = f.readLine();
while (line != null)
{
Logger.d(line);
stringBufferEula.append(line);
line = f.readLine();
}
}
catch (IOException e)
{
Logger.e(e.toString());
e.printStackTrace();
}
mTextViewTermsConditions.setText(stringBufferEula);
This is an encoding problem. Make sure that your asset file is stored with UTF-8 encoding.
Answer thanks to Ted Hopp above:
Make sure that the text file is encoded UTF-8.

Categories

Resources