What is the best way to view large text files from websites? - android

I tried webview.loadurl("www.blahblah.com/blah.txt") and it crashed after a couple of seconds. What else can I do?
I only want to view these .txt files from the web as fast and as stable as possible.
Also I came across asynctask while researching. What is it and can it help me with this?
Also how can I make them to a string if I want to display these text files as a listview item?

Asynctask is wrapper for a new thread that allow you to perform network tasks such as downloading. If you try to download on the UI thread, you will get a crash.
For the text file, download it to disc first, and then read it into memory and display it
StringBuilder txtString = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String textLine;
while ((textLine = br.readLine()) != null) {
txtString.append(line);
txtString.append('\n');
}
}
catch (IOException e) {
//error stuff
}

Related

Reading a large text file with over 130000 line of text

How can i read a large text file into my Application?
This is my code but it does not work. My code must read a file called list.txt. The code worked only with a file with only 10.000 lines.
can someone helps me?
Thanks!
My code:(Worked with small files, but not with large files)
private void largefile(){
String strLine2="";
wwwdf2 = new StringBuffer();
InputStream fis2 = this.getResources().openRawResource(R.raw.list);
BufferedReader br2 = new BufferedReader(new InputStreamReader(fis2));
if(fis2 != null) {
try {
LineNumberReader lnr = new LineNumberReader(br2);
String linenumber = String.valueOf(lnr);
while ((strLine2 = br2.readLine()) != null) {
wwwdf2.append(strLine2 + "\n");
}
// Toast.makeText(getApplicationContext(), linenumber, Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), wwwdf2, Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Since you are processing a large file, you should process the data in chunks . Here your file reading is fine but then you keep adding all rows in string buffer and finally passing to Toast.makeText(). It creates a big foot-print in memory. Instead you can read 100-100 lines and call Toast.makeText() to process in chunks. One more thing, use string builder instead of string buffer go avoid unwanted overhead of synchronization. You initializing wwwdf2 variable inside the method but looks it is a instance variable which I think is not required. Declare it inside method to make it's scope shorter.

Storing multi color text using a file in android

I'm currently working on an android project which allows user to write text using different colors and store them for later use(i.e., editing or reading).
Is their any way to store a file in android with multi color text ??
NOTE: I googled out for the solution but can't find anything useful.
I'm guessing the user has to perform some action to switch color?
If so - you can use that trigger to store the text position/length when switching and save a list of text position - color.
A commenter suggested HTML, and that may be a good choice. You are welcome to try Html.fromHtml() to populate your EditText with the contents of a simple HTML-formatted file, and you are welcome to try Html.toHtml() to generate HTML from the contents of your EditText. However, historically, those methods were not written to do a good job of implementing a "round trip", meaning that the contents of the EditText may wind up changing from its starting point to what it contains after doing Html.toHtml() (to generate and save the HTML) and Html.fromHtml() (to populate the EditText with the previously-saved HTML). If they do not work, you can either fork that Html class and try to modify it as needed, or write your own code to take a Spanned object and convert it to/from HTML, by examining the spans and generating HTML tags from them.
PROBLEM SOLVED:
Code for storing a multi color text from EditText to a txt file:
protected void onDestroy() {
super.onDestroy();
boolean writing_allowed= ExternalStorageWriting.isWritingPossible();
if(writing_allowed)
{
String store= Html.toHtml(et.getEditableText());
File myExternalFile = new File(getExternalFilesDir(filepath), filename3);
try {
FileOutputStream fos = new FileOutputStream(myExternalFile);
fos.write(store.getBytes());
fos.close();
} catch (Exception e) {
Toast.makeText(Notes.this, "Something went wrong...", Toast.LENGTH_SHORT).show();
}
}
}
Code for reading that txt file and displaying it in EditText :
private void setNotes()
{
String myData="";
try {
File myExternalFile = new File(getExternalFilesDir(filepath), filename3);
FileInputStream fis = new FileInputStream(myExternalFile);
DataInputStream in = new DataInputStream(fis);
BufferedReader br =
new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
myData = myData + strLine;
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
Spanned htmlText = Html.fromHtml(myData);
et.setText(htmlText);
}

I can't read .txt files from the raw folder

I am trying to learn how basic operations work in android apps. I have a .txt file in row folder and I can't read anything. Because when I execute the code (although I don't get any logcat errors) after one second, the emulator turns into a black screen.
String str="";
InputStream is=getResources().openRawResource(R.raw.readme);
StringBuilder finalstring=new StringBuilder();
BufferedReader bf=new BufferedReader(new InputStreamReader(is));
try {
while(str!=bf.readLine()){
finalstring.append(str);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
TextView tv=(TextView)findViewById(R.id.tvli);
tv.setText(finalstring);
Your while loop appears to be the problem. The condition in it doesn't really make sense. Try:
while((str = bf.readLine()) != null){
finalstring.append(str);
}
Your current loop will never run, as it will evaluate to "while str doesn't equal the line form my text file"
Replace your while statement with this:
while((str = bf.readLine()) != null) {
finalString.append(str);
}

Rooted Android Background onTouchListener

I'm wonder if there is any way (on rooted phone) to use onTouch method from background, do some think and then dispatch this touch to foreground application.
Create a process and throw this at it: getevent
Multiple new lines will come in every time the screen is touched. Must have root since it contains sensitive touch position information.
Something like this:
try {
Process process = Runtime.getRuntime().exec("su getevent"); //su to get root access
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = "";
while ((line = bufferedReader.readLine()) != null) {
//A new line came in. So a touch event came in.
}
}
catch (IOException e) {}
Note: I haven't tested it but it should work. Maybe minor tweaks are necessary.

own csv reader to string array out of memory

because i am only reading a very simple csv, where only strings are comma separated and should be converted to a String[].
I thought this was so easy a external jar would be a bit to much and i could handle this very easy. But what happens is that the first item get added until the memory is full and crash!
public List readWinkels(Activity a){
List winkelList = new ArrayList();
try{
InputStream winkelcsv = a.getResources().getAssets().open("winkels.csv");
BufferedReader br = new BufferedReader(new InputStreamReader(winkelcsv, "UTF-8"));
String s = br.readLine();
while (s != null){
winkelList.add(s);
System.out.println(s.toString());
}
br.close();
for(int i =0;i<winkelList.size();i++) {
System.out.println(winkelList.get(i));
}
}catch(IOException ioe){
ioe.printStackTrace();
}
return winkelList;
here is my code.... i dont get why it doesnt work, can anyone help? A readline reads the line and then the reading points jumps to the next line (i think) so why is the first line added a zillion times?
Here's the standard idiom for using a while loop to iterate over lines of a file, applied to your code:
String s;
while ((s = br.readLine()) != null){
winkelList.add(s);
System.out.println(s.toString());
}
You need to call readLine() at every iteration through the loop. The original code is nothing but an infinite loop, since s is only read once. Assuming s is not null, the loop condition is never false, so the list grows without bound until all available memory is used.

Categories

Resources