Read an already opened file using FileReader - android

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();
`

Related

Xamarin Android WebView - storing local content outside assets possible?

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");

Local Web Server with NanoHTTPD

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.

Where to put a file in android so it can be opened? [duplicate]

This question already has answers here:
How do I read the file content from the Internal storage - Android App
(6 answers)
Closed 6 years ago.
I want to open a file in android using the following code:
FileInputStream fis = openFileInput("examplelist.txt");
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader bufferedReader = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
sb.append(line);
}
but where to put the file examplelist.txt? I found some contradicting sources, like this and this? How to do it correctly?
Update:
I want to be able to replace this file at anytime after the app has been installed. So this file is NOT part of the source code or anything. It contains dynamical data...
I would have used a simple 'File Selector', so the user can navigate to the file anywhere on the android phone, but this looks terribly complicated and cumbersome. So, for now, I just want to open a file I can put somewhere on the phone in the most possible simple manner...
In order to be able to access a file, you should place it in /app/src/main/assets/examplelist.txt. You can access this directory in the Project explorer in Android Studio.
To open the file from java code, you can create an InputStream object by calling getAssets().open("examplelist.txt")
One possible way to solve this problem is the following:
You put the file in the kind-of-root folder, which might have the following path: /storage/emulated/0 (this is the location you can see when you connect the device e.g. to Linux and open it in the file explorer).
You can open the content of the file using the following code:
String path = Environment.getExternalStorageDirectory().toString();
Log.d("Files", "Path: " + path);
String joinedPath = new File(path, "examplelist.txt").toString();
try {
FileInputStream fis =new FileInputStream(new File(joinedPath));
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader bufferedReader = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
sb.append(line);
}
Log.d("Text", sb.toString());
} catch (IOException e) {
Toast.makeText(getBaseContext(), "File does not exist", Toast.LENGTH_LONG).show();
}
VERY IMPORTANT, and which I forget EVERY single time: You have to set permission to the app to access the storage. You have to put the permission tag in the AndroidMainfest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
...
but where to put the file examplelist.txt?
Your app would create the file itself. openFileInput() is part of internal storage, to which ordinary users have no access.
I would have used a simple 'File Selector', so the user can navigate to the file anywhere on the android phone, but this looks terribly complicated and cumbersome.
Step #1: Call startActivityForResult() with an ACTION_OPEN_DOCUMENT or ACTION_GET_CONTENT Intent (the former is for Android 4.4+)
Step #2: In onActivityResult(), if you get RESULT_OK as a result, get the Uri to the user's chosen content via getData() on the Intent passed into onActivityResult()
Step #3: Call openInputStream() on a ContentResolver to get an InputStream to use for reading the content
It's about 10-15 lines of code over what you have in your question, plus the code that your question needs but does not show (e.g., background thread for I/O).
Or, use a file chooser library.
I just want to open a file I can put somewhere on the phone in the most possible simple manner...
Use external storage, such as getExternalFilesDir().

textview showing the path not text of txt file

I want to use a text file in my app and I use this code textview1.set Text(R.raw.ludo); but in the app I only see the path of this file I want to use the text that is in it?
The parameter to setText() is a String which is set to it. In your case it is setting it to path of the file in raw folder as that is the value whihc has been assigned to that variable in R.java folder.
If you want to set content, then open the file in your app, read the contents in a string and set that string.
One of the ways to read is:
InputStream inputStream = getResources().openRawResource(R.raw.yourtextfile);
BufferedReader bufferedReader= new BufferedReader(new InputStreamReader(inputStream));
String eachline = bufferedReader.readLine();
while (eachline != null) {
........
}
you need to read the text from file.
Reading this thread can help you :
Reading a plain text file in Java

write data in binary format in Files in Android

hi all:
making a sample application in which we can store data on File and then could Read it.,
m using the following code.:
Code to Write:
OutputStreamWriter out = null;
try {
out = new OutputStreamWriter(openFileOutput("finear.fin", 0));
out.write(data); //data is String variable
out.close();
Toast.makeText(getApplicationContext(), "Data has been Saved! ", Toast.LENGTH_LONG).show();
}
Code to Read:
instream = openFileInput("finear.fin");
if(instream!=null)
{
InputStreamReader inputReader = new InputStreamReader(instream);
BufferedReader buffreader = new BufferedReader(inputReader);
String dataRead = buffreader.readLine();
}
Now problem is: The Data is stored in simple Text Form but i want that no one could read my data from that file and it should store the data in some binary form or some symbols type format so that when one opens the file in windows it should not display my text stored in the file or should display some other data.. BUT also that data could be readable in String form when i read. PLease Help
You can use Android's internal storage, which is private to your application:
You can save files directly on the device's internal storage. By default, files saved to the internal storage are private to your application and other applications cannot access them (nor can the user). When the user uninstalls your application, these files are removed.
Here you can find the docs: http://developer.android.com/guide/topics/data/data-storage.html#filesInternal
Encrypt the String before saving it to the stream and the other way to read it. If you want your file's contents to be unreadable, encryption would be a win.

Categories

Resources