I am reading blobs with size between 100kb and 1000kb from SQlite in my Android App using the following code :
public Object getCachefromDb(String sIdentifier){
String sSQL = " Select cache from cachtable where identifier='" + sIdentifier + "'";
Cursor c = null;
try {
c = connection_chronica.rawQuery(sSQL, null);
} catch (SQLiteException e) {
Log.v("SQLite Excetion", e.getMessage());
}
c.moveToFirst();
Log.v("DEBUG load Cache","sIdentifier : " + sIdentifier);
byte[] bData=null;
try{
bData = c.getBlob(0);
}catch(Exception e){
e.printStackTrace();
}
Object o = null;
if (bData!=null){
ByteArrayInputStream bos = new ByteArrayInputStream(bData);
ObjectInputStream ois;
try {
ois = new ObjectInputStream(bos);
o=ois.readObject();
} catch (StreamCorruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
c.close();
return o;
}
I would like to optimize the speed of reading and I found articles mentoining simpleQueryForBlobFileDescriptor.
My question : Does this help me reading BLOBS faster ? and if so how can I use it ?
Example from other posts:
SQLiteStatement get = mDb.compileStatement(
"SELECT blobColumn" +
" FROM tableName" +
" WHERE _id = 1" +
" LIMIT 1"
);
ParcelFileDescriptor result = get.simpleQueryForBlobFileDescriptor();
FileInputStream fis = new FileInputStream(result.getFileDescriptor()); // read like any other
My Tests results says its slower.
After long testing I found out, that using simpleQueryForBlobFileDescriptor is slower. See following code. My old code for example reads a blob in 390 miliseconds and the new code with simpleQueryForBlobFileDescriptor reads the same blob in 805 ms. I read somewhere that
simpleQueryForBlobFileDescriptor should be very fast for blob readings, but this seems not to be in my tests. Perhabs I am not doing it properly ? ( I hope so ). Any other hints.
public Object getCachefromDb_old(String sIdentifier){
Log.v("DEBUG LOAD BLOB","Start : " + sIdentifier);
Object o = null;
try {
// Erstelle ein SQLStatemant mit einer InClause
String sSQL = " Select cache from cachetable where identifier='" + sIdentifier + "'";
SQLiteStatement get = connection_chronica.compileStatement(sSQL);
ParcelFileDescriptor result = get.simpleQueryForBlobFileDescriptor();
FileInputStream fis = new FileInputStream(result.getFileDescriptor());
ObjectInputStream inStream = new ObjectInputStream(fis);
o=inStream.readObject();
} catch (StreamCorruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.v("DEBUG LOAD BLOB","End : " + sIdentifier);
return o;
}
Related
i am facing small problem while using OPENCSV and trying to read specific column from a line. I have a csv file that looks like this
"ID","Name","Name2","Date","Author"
"1","Alex","Example","18.3.2016","Alex"
Now i want to read only the column 2 and 3 (Name and Name2).
My code looks like this
try {
CSVReader reader = new CSVReader(new FileReader(filelocation));
String [] nextLine;
int rowNumber = 0;
while ((nextLine = reader.readNext()) != null) {
rowNumber++;
for(int i = 0; i< nextLine.length ; i++){
System.out.println("Cell index: " + i);
System.out.println("Cell Value: " + nextLine[i]);
System.out.println("---");
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I already tried setting the "i" variable manually to 1 and 2. But then i am getting 4 same results shown in log. What is missing? Thanks!
Never mind, i found the trick.
Here is how it should look.
try {
CSVReader reader = new CSVReader(new FileReader(filelocation));
String [] nextLine;
int rowNumber = 0;
while ((nextLine = reader.readNext()) != null) {
rowNumber++;
String name = nextLine[1];
String name2 = nextLine[2];
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
After it, i could get the string value and work with it.
I would like to make a quiz program. The questions are in a text file in asset folder. The answers are also in the asset folder called the number of the question (for example: the first question answers are in the text file called 1). I would like to give the questions and answers randomly (answers to a button). Until this everything is all right (maybe not the shortest solution, but works well). Then the user can answer the question clicking the correct button. And here is the problem. I get the text of the button and the first row of the answer file (always the first row is the right answer in the answer text file). It should be the same, and then I sign, this is the correct answer. But it's not the same, and I don't know why. A put text to the button from the answer file and get the first row from the answer file, so it should be the same. I print it out to log cat, and look like they are the same. I don't know what could be went wrong.
Can anybody help me out.
This is where I set the text of the button (randomly) and compare the first rows and the text of the button:
BufferedReader br2 = new BufferedReader(is2, 8192);
for(int k2=0; k2<3; k2++){
try {
kerdes2[k2] = br2.readLine();
final ArrayList <Integer> kerdesno2 = new ArrayList<Integer>();
for(int j=0;j<3;j++) kerdesno2.add(j);
Collections.shuffle(kerdesno2);
System.out.println(kerdesno2);
answ.setText(kerdes2[kerdesno2.get(0)]);
answ2.setText(kerdes2[kerdesno2.get(1)]);
answ3.setText(kerdes2[kerdesno2.get(2)]);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
answ.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
InputStreamReader is3 = null;
try {
is3 = new InputStreamReader(am.open(i3), "ISO-8859-1");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedReader br3 = new BufferedReader(is3, 8192);
try {
String helyes = br3.readLine();
System.out.println(helyes);
String gomb = answ.getText().toString();
System.out.println(gomb);
for(int f=0; f<helyes.length(); f++)
{
char c = helyes.charAt(f);
char d = gomb.charAt(f);
if(c != d){
System.out.println(c);
System.out.println(((String) gomb).indexOf(c));
}
}
if(gomb == helyes)
{
x++;
TextView eredmeny = (TextView)findViewById(R.id.eredmeny);
eredmeny.setText("Eredményed: " + Math.round(x*100/i2) + "%");
}
else
{
TextView eredmeny = (TextView)findViewById(R.id.eredmeny);
eredmeny.setText(gomb + " = " + helyes);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
answ2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
InputStreamReader is3 = null;
try {
is3 = new InputStreamReader(am.open(i3), "ISO-8859-1");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedReader br3 = new BufferedReader(is3, 8192);
try {
String helyes = br3.readLine();
System.out.println(helyes);
String gomb = answ2.getText().toString();
System.out.println(gomb);
if(gomb == helyes)
{
x++;
TextView eredmeny = (TextView)findViewById(R.id.eredmeny);
eredmeny.setText("Eredményed: " + Math.round(x*100/i2) + "%");
}
else
{
TextView eredmeny = (TextView)findViewById(R.id.eredmeny);
eredmeny.setText(gomb + " = " + helyes);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
answ3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
InputStreamReader is3 = null;
try {
is3 = new InputStreamReader(am.open(i3), "ISO-8859-1");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedReader br3 = new BufferedReader(is3, 8192);
try {
String gomb = answ3.getText().toString();
String helyes = br3.readLine();
System.out.println(gomb);
System.out.println(helyes);
if(gomb == helyes){
x++;
TextView eredmeny = (TextView)findViewById(R.id.eredmeny);
eredmeny.setText("Eredményed: " + Math.round(x*100/i2) + "%");
}
else
{
TextView eredmeny = (TextView)findViewById(R.id.eredmeny);
eredmeny.setText(gomb + " = " + helyes);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
As You can see I try to iterate over the two strings to realize where the problem is, but I couldn't manage to find...
String is an object. When comparing objects, use .equals(), not ==.
Your code:
if(gomb == helyes)
Should be:
if(gomb.equals(helyes))
By using == you're comparing memory, not the actual String objects. Sometimes you'll get the expected result, but other times you won't. .equals() will always test the Strings themselves.
I can see you are comparing by
if(gomb == helyes){
while it should be
if(gomb.equals(helyes)){
How to read the particular data from the internal storage file.
For eg., I have stored
1. Device 2. Time(epoch format) 3. button text
CharSequence cs =((Button) v).getText();
t = System.currentTimeMillis()/1000;
s = cs.toString();
buf = (t+"\n").getBytes();
buf1 = (s+"\n").getBytes();
try {
FileOutputStream fos = openFileOutput(Filename, Context.MODE_APPEND);
fos.write("DVD".getBytes());
fos.write(tab.getBytes());
fos.write(buf);
fos.write(tab.getBytes());
fos.write(buf1);
//fos.write(tab.getBytes());
//fos.write((R.id.bSix+"\n").getBytes());
fos.write(newline.getBytes());
//fos.flush();
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
then while reading, how can we read only price from the file? (using fos.read())
Thanks
I would suggest writing the file in a more structured way, like this:
long t = System.currentTimeMillis()/1000;
String s = ((Button) v).getText();
DataOutputStream dos = null;
try {
dos = new DataOutputStream(openFileOutput(Filename, Context.MODE_APPEND));
dos.writeUTF("DVD");
dos.writeLong(t); // Write time
dos.writeUTF(s); // Write button text
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (dos != null) {
try {
dos.close();
} catch (IOException e) {
// Ignore
}
}
}
To read it back, something like this:
DataInputStream dis = null;
try {
dis = new DataInputStream(openFileInput(Filename));
String dvd = dis.readUTF();
long time = dis.readLong();
String buttonText = dis.readUTF();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (dis != null) {
try {
dis.close();
} catch (IOException e) {
// Ignore
}
}
}
I want my app to hold data from ArrayList in file in between sessions. Class I use implements Serializable. When I debug saving seems to go OK, with no exceptions thrown, and going through the loop the right amount of times. Loading loads just some of entries and then throws EOF Exception. The code is here:
public int saveChildren(Context context){
FileOutputStream fos;
ObjectOutputStream os;
try {
fos = context.openFileOutput(filename, Context.MODE_PRIVATE);
os = new ObjectOutputStream(fos);
for(Child c : children){
os.writeObject(c);
}
os.close();
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return 1;
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return 2;
}
return 0;
}
public void loadChildren(Context context){
FileInputStream fis;
try {
fis = context.openFileInput(filename);
ObjectInputStream is;
is = new ObjectInputStream(fis);
while(is.readObject() != null){
Child c = (Child) is.readObject();
boolean skip = false;
for(Child ch: children){
if(ch.getName().equals(c.getName())){
skip = true;
}
if(ch.getNr().equals(c.getNr())){
skip = true;
}
if(ch.getImei() != null){
if(ch.getImei().equals(c.getImei())){
skip = true;
}
}
}
if(!skip){
children.add(c);
}
}
is.close();
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (StreamCorruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}catch (OptionalDataException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
What causes the errors?
You can directly write the children object into the ObjectOutputStream. ArrayList implements serializable. A second thing you might want to do is flush the stream before closing it with os.flush(). You will have:
os = new ObjectOutputStream(fos);
os.writeObject(children);
os.flush();
os.close();
and for reading:
is = new ObjectInputStream(fis);
ArrayList<Child> children = (ArrayList<Child>)is.readObject();
is.close();
You're using ObjectInputStream#readObject() returning null as an exit condition for your loop. #readObject() can return null - if you've serialized a null - it will however throw an exception if it fails to read an object because you've reached the end of the stream.
For a quick and easy fix: Consider serializing the length of the array as an integer before serializing the children & use a for-loop or similar construct to read the values - or serializing the ArrayList itself. It's also Serializable.
I am using the following code to read contacts and create a vcard file.
String lookupKey = cur.getString(cur.getColumnIndex(Contacts.LOOKUP_KEY));
Uri uri=Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
try {
fd = cr.openAssetFileDescriptor(uri, "r");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fis = fd.createInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] buf = new byte[(int)fd.getDeclaredLength()];
try {
if (0 < fis.read(buf))
{
vCard = new String(buf);
writer.write(vCard);
writer.write("\n");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
But while going through the list of contacts, I get the error:
ERROR/MemoryFile(284):
MemoryFile.finalize() called while
ashmem still open.
And my generated .vcf file is missing some contacts and also does not end properly.
Can someone please tell me what is wrong with my code.
You need close stream fis
try {
fis = fd.createInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] buf = new byte[(int)fd.getDeclaredLength()];
try {
if (0 < fis.read(buf))
{
vCard = new String(buf);
writer.write(vCard);
writer.write("\n");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Close stream
fis.close();
I had the same issue. I used a open source android-vcard jar to write the contacts to vcard.