Nima Khalili
Nima Khalili

Reputation: 414

show image from database (Bitmap error)

i have to button : first button is for save image from gallery to database , second button is for show images from database in imageView but My problem with the second button is the error this line : "Bitmap bitmapImage = BitmapFactory.decodeFile(imageFile.toString());"

in logcat error :

"E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: [B@c97f202: open failed: ENOENT (No such file or directory)"

Database code :

private static String dbNAME = "myDB.db";
    private static String tableName = "imageColumns";
    private static String imageColumnName = "image";
    private Bitmap bitmap;
    private int i = 1;

    db = this.openOrCreateDatabase(dbNAME, SQLiteDatabase.CREATE_IF_NECESSARY, null);
    db.setVersion(1);
    db.setLocale(Locale.getDefault());

    String createTable = "create table if not exists imageColumns(id INTEGER PRIMARY KEY AUTOINCREMENT , image TEXT)";
    db.execSQL(createTable);

First Button(save Image) code :

    public void save(View view) {
    if (bitmap != null) {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 0, stream);
        byte[] byteImage = stream.toByteArray();

        ContentValues values = new ContentValues();
        values.put(imageColumnName, String.valueOf(byteImage));

        db.insert(tableName, null, values);

        Toast.makeText(this, "Save", Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(this, "Save Image First!", Toast.LENGTH_SHORT).show();
    }


}

Second Button(show Image) code :

public void showImage(View view) {

    Cursor cursor2 = db.rawQuery("select * from imageColumns where id = " + i, null);
    if (cursor2.getCount() != 0) {
        while (cursor2.moveToNext()) {
            Toast.makeText(this, "number of images" + i, Toast.LENGTH_SHORT).show();
            String path = cursor2.getString(cursor2.getColumnIndex(imageColumnName));
            File imageFile = new File(path);
            Bitmap bitmapImage = BitmapFactory.decodeFile(imageFile.toString());
                 imageView.setImageBitmap(bitmapImage); //this line is problem
       }
        i++;
    }

}

Upvotes: 0

Views: 351

Answers (1)

Suraj Vaishnav
Suraj Vaishnav

Reputation: 8305

public byte[] convetBitmapToByteArray(Bitmap bm)
{
    int size = bm.getRowBytes() * bm.getHeight();
    ByteBuffer byteBuffer = ByteBuffer.allocate(size);
    bm.copyPixelsToBuffer(byteBuffer);
   return byteBuffer.array();
}
public Bitmap convertByteToBitmap(byte[] evidence)
{
    Bitmap bmp= BitmapFactory.decodeByteArray(evidence, 0, evidence.length);
    return bmp;
}

Use above two methods to store and retrieve image to table. You should make a column of BLOB type in table which can store byte array so first you have to convert bitmap to byte array and then insert it to table, when you retrieve record you will get that byte array, so convert it back to bitmap to set it to imageView.

Upvotes: 1

Related Questions