I am trying to develop dictionary application which uses source files from res/raw.
It works when i use a smaller file, but not with a file that I have to use which is cca 5mb.
I decided to split it in parts and to use it this way because it appears that android has some file size limits for assets folder(1mb?).
But it doesnt work this way, it loads something in the program but not all the dictionary files. What seems to be the problem? Am I going in a completely wrong way about this?
private void loadWords() throws IOException {
final Resources resources = mHelperContext.getResources();
InputStream inputStream = resources.openRawResource(R.raw.definitions1);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
ucitaj(reader);
InputStream inputStream2 = resources.openRawResource(R.raw.definitions2);
BufferedReader reader2 = new BufferedReader(new InputStreamReader(inputStream2));
ucitaj(reader2);
InputStream inputStream3 = resources.openRawResource(R.raw.definitions3);
BufferedReader reader3 = new BufferedReader(new InputStreamReader(inputStream3));
ucitaj(reader3);
InputStream inputStream4 = resources.openRawResource(R.raw.definitions4);
BufferedReader reader4 = new BufferedReader(new InputStreamReader(inputStream4));
ucitaj(reader4);
InputStream inputStream5 = resources.openRawResource(R.raw.definitions5);
BufferedReader reader5 = new BufferedReader(new InputStreamReader(inputStream5));
ucitaj(reader5);
InputStream inputStream6 = resources.openRawResource(R.raw.definitions6);
BufferedReader reader6 = new BufferedReader(new InputStreamReader(inputStream6));
ucitaj(reader6);
--
private void ucitaj(BufferedReader reader) throws IOException {
Log.d(TAG, "Loading words...");
try {
String line;
while ((line = reader.readLine()) != null) {
String[] strings = TextUtils.split(line, " -- ");
if (strings.length < 2) continue;
long id = addWord(strings[0].trim(), strings[1].trim());
if (id < 0) {
Log.e(TAG, "unable to add word: " + strings[0].trim());
}
}
} finally {
reader.close();
}
Log.d(TAG, "DONE loading words.");
}
I'm pretty sure - that chunking into several resources is useless, since anyway you're reading in the end all files into RAM/heap, which size is limited. In resembling situation (I need to show in TextView relatively large text) I had overcome situation using special class which was implementing CharSequence interface. In fact CharSequence has several abstract methods which can be used for chunking/positioning in big file for necessary fragments, like:
abstract char charAt(int index)
abstract CharSequence subSequence(int start, int end)
Probably in your situtation it might help.
Related
I am trying to download the json file which contains slovenian characters,While downloading json file as a string I am getting special character as specified below in json data
"send_mail": "Po�lji elektronsko sporocilo.",
"str_comments_likes": "Komentarji, v�ecki in mejniki",
Code which I am using
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
try {
InputStream input1 = new BufferedInputStream(url.openStream(), 300);
String myData = "";
BufferedReader r = new BufferedReader(new InputStreamReader(input1));
StringBuilder totalValue = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
totalValue.append(line).append('\n');
}
input1.close();
String value = totalValue.toString();
Log.v("To Check Problem from http paramers", value);
} catch (Exception e) {
Log.v("Exception Character Isssue", "" + e.getMessage());
}
I want to know how to get characters downloaded properly.
You need to encode string bytes to UTF-8. Please check following code :
String slovenianJSON = new String(value.getBytes([Original Code]),"utf-8");
JSONObject newJSON = new JSONObject(reconstitutedJSONString);
String javaStringValue = newJSON.getString("content");
I hope it will help you!
Decoding line in while loop can work. Also you should add your connection in try catch block in case of IOException
URL url = new URL(f_url[0]);
try {
URLConnection conection = url.openConnection();
conection.connect();
InputStream input1 = new BufferedInputStream(url.openStream(), 300);
String myData = "";
BufferedReader r = new BufferedReader(new InputStreamReader(input1));
StringBuilder totalValue = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
line = URLEncoder.encode(line, "UTF8");
totalValue.append(line).append('\n');
}
input1.close();
String value = totalValue.toString();
Log.v("To Check Problem from http paramers", value);
} catch (Exception e) {
Log.v("Exception Character Isssue", "" + e.getMessage());
}
It's not entirely clear why you're not using Android's JSONObject class (and related classes). You can try this, however:
String str = new String(value.getBytes("ISO-8859-1"), "UTF-8");
But you really should use the JSON libraries rather than parsing yourself
When creating the InputStreamReader at this line:
BufferedReader r = new BufferedReader(new InputStreamReader(input1));
send the charset to the constructor like this:
BufferedReader r = new BufferedReader(new InputStreamReader(input1), Charset.forName("UTF_8"));
problem is in character set
as per Wikipedia Slovene alphabet supported by UTF-8,UTF-16, ISO/IEC 8859-2 (Latin-2). find which character set used in server, and use the same character set for encoding.
if it is UTF-8 encode like this
BufferedReader bufferedReader= new BufferedReader(new InputStreamReader(inputStream), Charset.forName("UTF_8"));
if you had deffrent character set use that.
I have faced same issue because of the swedish characters.
So i have used BufferedReader to resolved this issue. I have converted the Response using StandardCharsets.ISO_8859_1 and use that response. Please find my answer as below.
BufferedReader r = new BufferedReader(new InputStreamReader(response.body().byteStream(), StandardCharsets.ISO_8859_1));
StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null)
{
total.append(line).append('\n');
}
and use this total.toString() and assigned this response to my class.
I have used Retrofit for calling web service.
I finally found this way which worked for me
InputStream input1 = new BufferedInputStream(conection.getInputStream(), 300);
BufferedReader r = new BufferedReader(new InputStreamReader(input1, "Windows-1252"));
I figured out by this windows-1252, by putting json file in asset folder of the android application folder, where it showed same special characters like specified above,there it showed auto suggestion options to change encoding to UTF-8,ISO-8859-1,ASCII and Windows-1252, So I changed to windows-1252, which worked in android studio which i replicated the same in our code, which worked.
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 am receiving huge JSON and and while I am reading the lines OutOfMemoryError appears.
Here is my first method that I am trying to parse the JSON.
InputStream in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"), 8);
String result = "";
while (true) {
String ss = reader.readLine();
if (ss == null) {
break;
}
result += ss;
}
And I've also tried this method.
InputStream in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ( (line = reader.readLine()) != null)
{
sb.append(line);
}
In the both of the cases the OutOfMemoryErorr is appearing.
Mostly the error will occur due to heap size. You need to increase the size of the heap
To increase the heap size
For additional info on heap size in java visit here
The best solution I found was to raise the Heap of the Application.
I placed android:largeHeap="true" under the <application/> in the AndroidManifest.xml.
When you instantiate your BufferedReader, you pass in an int size of 8, meaning your buffer is 8 characters (I assume that's not what you want). If you pass no size, the default is 8192 characters.
Try this:
BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
Or this (same effect):
BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"), 8192);
http://developer.android.com/reference/java/io/BufferedReader.html
I'm not completely sure that will fix the error you are seeing, but worth trying if you can't get it working.
My JSON code was waiting for status, which comes towards the end. So I modified the code to return earlier.
// try to get formattedAddress without reading the entire JSON
String formattedAddress;
while ((read = in.read(buff)) != -1) {
jsonResults.append(buff, 0, read);
formattedAddress = ((String) ((JSONObject) new JSONObject(
jsonResults.toString()).getJSONArray("results").get(0))
.get("formatted_address"));
if (formattedAddress != null) {
Log.i("Taxeeta", "Saved memory, returned early from json") ;
return formattedAddress;
}
}
JSONObject statusObj = new JSONObject(jsonResults.toString());
String status = (String) (statusObj.optString("status"));
if (status.toLowerCase().equals("ok")) {
formattedAddress = ((String) ((JSONObject) new JSONObject(
jsonResults.toString()).getJSONArray("results").get(0))
.get("formatted_address"));
if (formattedAddress != null) {
Log.w("Taxeeta", "Did not saved memory, returned late from json") ;
return formattedAddress;
}
}
As the title says, I'm looking for a short function that reads in a file if there is a particular string and, //do something if present. For example if in the test.txt file is present the string TestString.
Someone would be so kind to suggest me a method?
InputStream is = //--open an input stream from file--
BufferedReader rd = new BufferedReader(new InputStreamReader(is,"UTF-8"));
String line;
while ( (line = rd.readLine()) != null ){
if(line.matches(".*TestString.*")){ //--regex of what to search--
//--do something---
break; //--if not want to search further--
}
}
I am currently reading a dictionary.png (which is a text file) into a buffer.
.png to make android think it is compressed.
InputStream is = getResources().openRawResource(R.raw.dictionary);
try {
BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF8"));
String next;
while ((next = br.readLine()) != null) {
//Add the string next to a map or whatever
}
} catch (Exception e) {
//Something nasty happened
}
i'm not very familiar with HashMaps but i want to read the files from the dictionary into a hash map.
Any help would be appreciated.
An example that does something similar to what you're asking about: Using HashMap to map a String and int