0

I'm learning about databases in android and have come across the following question, I would like to retrieve an image, how can I change the code below to image instead of string?

public List<String> getImage(){
    List<String> listRows = new ArrayList<String>();
    SQLiteDatabase db = this.getWritableDatabase();

    Cursor c;
    try {
        c = db.rawQuery("SELECT image FROM " + MY_TABLE + " WHERE _id = "  + _id, null);
        if(c == null) return null;

        String row;
        c.moveToFirst();
        do {            
            row = c.getString(0);            
            listRows.add(row);
        } while (c.moveToNext()); 
        c.close();
    }
    catch (Exception e) {
        Log.e("LOGmsg getDBImage", e.getMessage());
    }

    db.close();        

    return listRows;
}   
ArchiFloyd
  • 1,274
  • 13
  • 20
TwoStarII
  • 351
  • 3
  • 17
  • More information on how your database is set up would be helpful. – mkorcha Mar 02 '15 at 18:10
  • Normally, you store images as byte arrays inside your database. BitmapFactory.decodeByteArray() could help you to decode them again. Check out this question: https://stackoverflow.com/questions/7331310/how-to-store-image-as-blob-in-sqlite-how-to-retrieve-it – flxapps Mar 02 '15 at 18:12

2 Answers2

2

store the image in the database is a bad practice, it is better to store pictures on the sd and store in the database paths to them

gadfil
  • 417
  • 4
  • 13
  • I just spoke to a colleague at work and was informed exactly the same. I will approach this method of storing the path in the DB rather than the image as a BLOB. Thanks. – TwoStarII Mar 03 '15 at 08:06
0

Treat the image as a byte array and write/retrieve it to/from your database as a Blob object.

Michael Powell
  • 746
  • 4
  • 11