I have just started with Android development, and facing one issue in the android application.
When application tries to read the data from file (raw resources), it throws IOException on readLine() function. code is as below:
final Resources resources = m_ApplicationContext.getResources();
InputStream inputStream = resources.openRawResource(R.raw.datafile);
BufferedReader reader = new BufferedReader(new InputStreamReader(
inputStream));
try {
String line;
while ((line = reader.readLine()) != null) {
the reader.readLine() function is throwing the exception. Do I need to mention any kind of additional permission for reading the file ?
Thanks in advance.
I have had the same issue. The problem seems to be that you can only read files with up to 1MB
see here
http://groups.google.com/group/android-developers/browse_thread/thread/e3765c112d112f24
Related
Is it possible to exec commands in a Xamarin application? Consider the following:
var process = Java.Lang.Runtime.GetRuntime().Exec("logcat");
var hasExited= await process.WaitForAsync();
I would like to be able to take the results of process and read it to a string. This can be done in native Android, but I am looking for a Xamarin C# solution. Any help is greatly appreciated.
Xamarin.Android is based on native Android.So you can call this method in Xamarin.Android .
var process = Java.Lang.Runtime.GetRuntime().Exec("getprop");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.InputStream));
StringBuilder builder = new StringBuilder();
string line;
while ((line = bufferedReader.ReadLine()) != null)
{
builder.Append(line + "\n");
}
System.Console.WriteLine(builder.ToString());
I'm currently learning android with a books called "Android Programming - The Big Nerd Ranch Guide".
As a part of a learning project we create Json serializer for saving and loading data. Writing the file appearently works fine, and I get no error messages on the Logcat. After I terminate the app and recreate it, the data loader is called and raises the following exception:
org.json.JSONException: End of input at character 0
I've looked for this issue online and figured it's probably because the BufferedReader returns an empty response. I've checked and indeed it is the case.
For simplicity sake, I've temporarily put a BufferedReader into the saving function and tried reading the info I've just saved into the file, and still the BufferedReader returns only null.
public void saveCrimes(ArrayList<Crime>crimes)
throws JSONException, IOException {
JSONArray array = new JSONArray();
for(Crime c: crimes)
array.put(c.toJSON());
Writer writer = null;
try {
OutputStream out = mContext.openFileOutput(mFileName, Context.MODE_PRIVATE);
writer = new OutputStreamWriter(out);
writer.write(array.toString());
Log.d(TAG, array.toString());
} finally {
if(writer == null)
writer.close();
}
// Extracting the data
BufferedReader bufferedReader = null;
try {
InputStream inputStream = mContext.openFileInput(mFileName);
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
if (bufferedReader.readLine() == null)
Log.d(TAG, "WHY GOD WHYYYYYYY");
}catch (IOException e){
}
}
(The two log messages from the code, the first one displays the data that is in the JsonArray I'm using)
D/CriminalIntentJSONSerializer: [{"date":"Mon May 14 17:33:08 GMT+00:00 2018","id":"97fe9532-991f-4352-9de1-602fa8dfa93e","isSolved":true,"title":""}]
D/CriminalIntentJSONSerializer: WHY GOD WHYYYYYYY
Would love to hear your insight.
BufferedReader bufferedReader = null;
try {
InputStream inputStream = mContext.openFileInput(mFileName);
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
if (bufferedReader.readLine() == null)
Log.d(TAG, "WHY GOD WHYYYYYYY");
}catch (IOException e){
}
Ok. you've created your BufferedReader bufferedReader = null;
What happens when yuou say bufferedReader = new BufferedReader(anything)...Well...it can't call a new instance the same thing that's already been declared...in fact, it's already been instantiated as null. So you can't create a new instance of the same name.
Try deleting the line where you point it at null. Then, substitute
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
for the original declaration in your try block
Try to check if mFilename is empty on second try-catch, usually in Android instance disappear easily.
PD: I advise you choice another JSON library to manipulte JSON files, they are lightweight and easy-to-use.
== Edit ==
Have you added writing and reading permissions on AndroidManifest?
If answer is "there it is" try to debug app step-by-step looking for variables and in-variables for checking existence of values.
Could be file isn't writing itself or it's writing empty.
Error basically is empty string or non-format JSON-like:
""
"[{"a": "abdc", "b": "jef2","
Paying attention to BufferedReader because it read lines each and you need all file and then join into string variable.
Also, try to use android file explorer that come in AndroidStudio. There you can explore files, logs and database files and export them to your specific folders (Documents, Downloads, etc). Generally files written by app are stored in data -> <com.your.app.package>.
I'm trying to get the content Text of a Message in my Android App with an InputStream, because there I can get a line Separator. I'm getting the following Exception when I'm trying it:
java.lang.ClassCastException: java.lang.String cannot be cast to java.io.InputStream
This is my Code:
Object o = message.getContent();
InputStream is = (InputStream)o;
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
String everything = sb.toString();
Do you know what the problem is? In every Javamail - Thread you can read that this Method runs.
Use the Message.getInputStream method.
OK, I'm officially frustrated with file handling on the Android. I'm new to it (only a few days) so I might just be missing something obvious. I have successfully written a short file of multiple lines with CSV data on each line. That's not the problem; reading is. I started with this (leaving out the exception handling and parsing for clarity):
FileInputStream in = context.openFileInput("foo.txt");
InputStreamReader isr = new InputStreamReader( in );
BufferedReader buffreader = new BufferedReader( isr );
String inline;
while( (inline = buffreader.readLine())!=null)
{
// parse CSV here
}
I got an immediate null when reading the line. So I worked back up the line and tried reading raw bytes using various examples in SO as a pattern. I won't reproduce all the fails, but things like this didn't work either:
FileInputStream in = context.openFileInput("foo.txt");
InputStreamReader isr = new InputStreamReader( in );
BufferedReader buffreader = new BufferedReader( isr );
CharBuffer buff = CharBuffer.allocate(1024);
isr.read(buff);
String s = buff.toString();
Debugging showed that the character buffer and string had consumed characters, but they were blank. Well, I can truly go old-school (I programmed in C before it was a decade old) if necessary. The following worked:
FileInputStream in = context.openFileInput("foo.txt");
InputStreamReader isr = new InputStreamReader( in );
int c;
String foo = "";
while( (c = isr.read())>=0)
foo += (char)c;
"foo" managed to have the whole contents which I can split up and process, but I dislike that it's so coarse. Yes, it works, but I'd like to know why the others didn't. I tried to stick closely to the various examples in SO, but had no success.
Any ideas about what might be wrong with the first attempts?
do {
inline=bruffreader.readLine();
if(inline != null) {
//parse here
}
} while (inline != null);
worked for me, it didn't return null
I have my textfile stored in assets folder and my requirement is to show the contents in textview pointwise.I am able to access the contents if there is no space in text file which is stored in assets folder.If I puts the space in the text file then i am not able to get the contents after space.How to achieve this means to show the contents pointwise.
for example my textfile is as follows
a)Americab)Africac)India
I want output as
a) America
b) Africa
c) India
Here is my code to access the text file from assest folder which I am getting.
InputStream in = this.getAssets().open("detailtext.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
line = reader.readLine();
Since you need to read multiple lines, you need to loop through, till you reach the EOF. Try something like this:-
StringBuffer sb = new StringBuffer(0);
InputStream in = this.getAssets().open("detailtext.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line = null;
while((line = reader.readLine()) != null){
sb.append(line);
sb.append("\n");
}
String wholeText = sb.toString();
You're only reading one line, you need to use a while loop and continue to read each line until the end of the file
InputStream in = this.getAssets().open("detailtext.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
while((line = reader.readLine()) != null){
// do whatever with line
}