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;
}   
2
  • More information on how your database is set up would be helpful. Commented Mar 2, 2015 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: stackoverflow.com/questions/7331310/… Commented Mar 2, 2015 at 18:12

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

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.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.