Home > Mobile >  BitmapFactory.decodeFile does not work for me
BitmapFactory.decodeFile does not work for me

Time:07-16

// Convert image to bitmap
Bitmap src1 = BitmapFactory.decodeFile(R.drawable.fried_chicken);

// Convert bitmap to byte
ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
src1.compress(Bitmap.CompressFormat.JPEG,100,baos1);
byte[] byte1 = baos1.toByteArray();

// Convert byte to blob
Blob blob1 = new SerialBlob(byte1);

When I do this, theres a red line for R.drawable.fired_chicken. Aren't I supposed to put the path file? Am I missing something? SerialBlob is also underlined, is this because of the previous error?

FYI: I am decoding an image into bitmap then to blob because I want to save it into a sqlite database.

image

CodePudding user response:

The red line is because you are trying to decode a drawable (by its id) instead of a File (by a String) to decode the drawable change the line to

Bitmap src1 = BitmapFactory.decodeResource(context.getResources(),R.drawable.fried_chicken);

For the SerialBlob error as far as I know there's no SerialBlob in Android Studio Try checking this how to store Image as blob in Sqlite & how to retrieve it?

  • Related