0

I have a SQLite database where I am inserting data directly which should appear as prestored data for my app user.My code is working fine for one image.But no idea to convert and retrieve an array of image

I tried Bitmap bitmap=BitmapFactory.decodeResource(getResources().pmge);.But I caused error to decodeResource () synatx

code

   public class MainActivity extends Activity {

Integer [] pmge ={R.drawable.candle1,R.drawable.candl3,
        R.drawable.candl4,R.drawable.candl5,R.drawable.candl6,
        R.drawable.lawn,R.drawable.sglc10,R.drawable.senson,R.drawable.thejus6669};


MyDataBase myDataBase;
SQLiteDatabase database;
Cursor c;
ImageView imageView;
byte[] img,img1;
Bitmap b;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ImageView imageView=(ImageView)findViewById(R.id.imge);
    myDataBase=new MyDataBase(getApplicationContext(),"imagedata",null,1);





                  Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.candle1);
                  ByteArrayOutputStream bos=new ByteArrayOutputStream();
                  bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
                  img=bos.toByteArray();

                  database=myDataBase.getWritableDatabase();


                  Bitmap b1=BitmapFactory.decodeByteArray(img, 0, img.length);
                  imageView.setImageBitmap(b1);
                }
}

class MyDataBase extends SQLiteOpenHelper{
public MyDataBase(Context context, String name, CursorFactory factory,
        int version) {
    super(context, name, factory, version);
    // TODO Auto-generated constructor stub
}

public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    db.execSQL("create table tableimage (image BLOB,);");
    db.execSQL("INSERT INTO tableimage(image) VALUES(img) ");
}

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub

}

}
panda_heart_3
  • 227
  • 2
  • 17
  • 1
    You should not store the database its wrong way, you should store the path of that images in your database. – Akshay Mukadam Feb 04 '15 at 05:14
  • path of image from where? – panda_heart_3 Feb 04 '15 at 05:18
  • The Image which you want to store must be available on device right? If thats the case you can get the path of that Image using File Class API and just store that path. Simple !! – Akshay Mukadam Feb 04 '15 at 05:21
  • I think this is what you are looking for [store images to database](http://stackoverflow.com/questions/9357668/how-to-store-image-in-sqlite-database) – SwANDp Feb 04 '15 at 05:25

5 Answers5

0

You have to use BLOB format to store image in Table.and you should convert Image as stream then store it.I hope this one will help you :)

user3515851
  • 469
  • 1
  • 4
  • 14
0

You can go with the Blob type .An SQL BLOB type stores a large array of binary data (bytes) as the value in a column of a database.

Or

You can store URI into database to get reference where are file are locate.

QuokMoon
  • 4,387
  • 4
  • 26
  • 50
  • I perfectly stored and retrieved one image from sqlite database.I have updated its code.But not able to convert an array of image. – panda_heart_3 Feb 04 '15 at 08:28
0

convert your image into Bitmap and than bitmap into a byte[]and after you can store it in your database as a Blob.

To convert a Bitmap into a byte[], you can use this :

Bitmap yourBitmap;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
yourBitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
byte[] bArray = bos.toByteArray();

and than for retrieving/getting image use

byte[] byteArray = DBcursor.getBlob(columnIndex);  
Bitmap bm = BitmapFactory.decodeByteArray(byteArray, 0 ,byteArray.length);
Fasiha
  • 488
  • 3
  • 11
  • okayz.What if I have more than one image.Plz look into my updated code.Can I do like that. – panda_heart_3 Feb 04 '15 at 06:00
  • make a int array Integer[] mThumbIds = { R.drawable.ic_launcher, R.drawable.ic_launcher }; and Create a table like this CREATE_EMPLOYEES_TABLE = "create table " + EMPLOYEES_TABLE + " (" + EMP_ID + " integer primary key autoincrement, " + EMP_PHOTO + " blob not null, " + EMP_NAME + " text not null unique, " + EMP_AGE + " integer );"; – Fasiha Feb 04 '15 at 06:31
0

No you cannot store images directly into database, there are various methods to do that. e.g

First you need to create a field of type BLOB in your table.

Second step convert your image to a Byte Array.

Finally store/push that Byte Array to that specific column that has data type BLOB in your database table

mushahid
  • 504
  • 5
  • 21
  • make a new table which has only two columns, one for ID and one for storing images, store as many images but the ID should be same for all of them mean you have to store both the ID as well as images yourself; consider a foreign key for that purpose. So when you query it will be like (Select All the images where ID == "a specific id") and in this way you can store multiple images for a single entry – mushahid Feb 04 '15 at 06:14
  • i don't understand what you mean by that can you explain what you mean or what you want to achieve? – mushahid Feb 05 '15 at 13:26
0

I would suggest not to store images in database directly. Just store image on specific path and store the path to that image in the database.

Check this answer: https://stackoverflow.com/a/3751/1739882

The reason for storing files in database is not preffered:

  1. You will require additional code to extract and stream the images from database
  2. Your database server will become heavy, and also your database, too.
Community
  • 1
  • 1
Chintan Soni
  • 24,761
  • 25
  • 106
  • 174