Hi I have created this method to do the folowing:
-it takes a bitmap
-creates temp file
-compress bitmap and put it in temp file
-open temp file
-read bytes >>>problem is here in my code
-save these bytes in sqlite db
-retrieve bytes from sqlite db
-set those bytes as image bitmap again
I know I know totally useless but this is the only way I have found to do what I want to achieve which is:
-compress a bitmap
-save it to sqlite database
so here is the code. the program just crashes at in.read(bb):
private void updateBitmapData() {
File file = null;
FileOutputStream stream = null;
try {
file = File.createTempFile("image", null, getBaseContext().getFilesDir());
stream = new FileOutputStream(file);
System.out.println(thumbnail.getRowBytes());
thumbnail.compress(Bitmap.CompressFormat.JPEG, 50, stream);
stream.close();
FileInputStream in=new FileInputStream(file);
byte[] bb = null;
in.read(bb); //crash
in.close();
System.out.println("despues compression"+bb.length);
DBAdapter db=new DBAdapter(getApplicationContext());
db.insertData(bb);
Cursor c=db.getLastImage();
if(c.moveToFirst()){
bb=c.getBlob(0);
}
c.close();
db.close();
im.setImageBitmap(BitmapFactory.decodeByteArray(bb, 0, bb.length));
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.e("NuevaIncidencia", e.getLocalizedMessage());
im.setImageBitmap(thumbnail);
}catch (IOException e1) {
Log.e("NuevaIncidencia", e1.getLocalizedMessage());
e1.printStackTrace();
im.setImageBitmap(thumbnail);
} catch (Exception e2){
e2.printStackTrace();
//Log.e("NuevaIncidencia", e2.getLocalizedMessage());
}
}
How could I solve this problem? thanks
I guess you got a NullPointerException, and it seems to be normal because your buffer is null when you call read.
You have to instantiate the buffer with the expected size.
Try initializing your byte array this way:
byte[] bb = new byte[file.length()];Hope it helps
Jokahero