-1

i have another problem in my app...

here the scenario : 1. i have sqlite file that i put in asset folder it contain title : VARCHAR and image : ?? (will be my question at this time i use VARCHAR)

  1. i have image that i store in res/drawable folder

  2. i set the value in sqlite, so my database already contain some value (title and image)

my question is

is it possible to set an image value from my sqlite so i can get the image from res/draweable folder?

if it possible, what type of data should i use? and how to call it when load the data base

if it not possible? what the best way to use the image?

at this time here my sqlite helper

public ArrayList<DrawerItem> getItemMenu(){

    ArrayList<DrawerItem> listMenu = new ArrayList<DrawerItem>();

    String selectQuery = "SELECT * FROM tbMenu";

    Cursor cursor = myDataBase.rawQuery(selectQuery, null);
    if(cursor.moveToFirst()){
        do{
            DrawerItem setMenu = new DrawerItem(selectQuery, Integer.parseInt(selectQuery));
            setMenu.setTitle(cursor.getString(0));
            setMenu.setImgResID(cursor.getInt(1));

            listMenu.add(setMenu);
        }while (cursor.moveToNext());
    }

    return listMenu;
}

thx for your time guys to read my question...

Wilson
  • 176
  • 1
  • 11
DumDum
  • 67
  • 7
  • Check this post: http://stackoverflow.com/questions/7331310/how-to-store-image-as-blob-in-sqlite-how-to-retrieve-it – Pedro Oliveira Jun 19 '14 at 10:27
  • _"is it possible to set an image value from my sqlite so i can get the image from res/draweable folder?"_ what are you trying to say? – SMR Jun 19 '14 at 10:34
  • i mean like hard code data base... i want to make sqlite with data already inside when app launch... so i don`t need to store from app... – DumDum Jun 20 '14 at 01:21

1 Answers1

0

you have to use "blob" to store image.

ex: to store a image in to db

public void insertImg(int id , Bitmap img ) {   


byte[] data = getBitmapAsByteArray(img); // this is a function

insertStatement_logo.bindLong(1, id);       
insertStatement_logo.bindBlob(2, data);

insertStatement_logo.executeInsert();
insertStatement_logo.clearBindings() ;

}

public static byte[] getBitmapAsByteArray(Bitmap bitmap) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0, outputStream);       
return outputStream.toByteArray();

}

to retrieve a image from db

public Bitmap getImage(int i){

String qu = "select img  from table where feedid=" + i ;
Cursor cur = db.rawQuery(qu, null);

if (cur.moveToFirst()){
    byte[] imgByte = cur.getBlob(0);
    cur.close();
    return BitmapFactory.decodeByteArray(imgByte, 0, imgByte.length);
}
if (cur != null && !cur.isClosed()) {
    cur.close();
}       

return null ;
} 
Wilson
  • 176
  • 1
  • 11
  • thx for the reply, but how if i don`t want to store it first? i want to write it directly from my sqlite editor (mozila sqlite tool) – DumDum Jun 19 '14 at 10:52