2

in my app I have set an image from the gallery in an image view. Now I want to store that image in my SQL database.

  1. What do I have to change in my SQL database? (BLOB??)
  2. What is the best way to retrieve the image from the image view and store it in the SQL Database?

Here is my SQL Database:

private static final String DATABASE_CREATE =
        "create table " + DATABASE_TABLE + " ("
                + KEY_ROWID + " integer primary key autoincrement, "
                + KEY_TITLE + " text not null, " 
                + KEY_BODY + " text not null, " 
                + KEY_DATE_TIME + " text not null);"; 

Here I get the path of the image in the gallery and then set the image in the image view

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == PICK_FROM_FILE) {
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);

            Log.v("IMAGE PATH====>>>> ",selectedImagePath);

         // Decode, scale and set the image.
            Bitmap myBitmap = BitmapFactory.decodeFile(selectedImagePath);
            Bitmap scaledBitmap = Bitmap.createScaledBitmap(myBitmap, NEW_WIDTH, NEW_HEIGHT, true);
            myBitmap.recycle();
            myBitmap = null;

            mImageView.setImageBitmap(scaledBitmap);

        }
    }
}
user1420042
  • 1,689
  • 9
  • 35
  • 53

3 Answers3

3

first create table like this db.execSQL("create table if not exists tablename(....,images BLOB,.....);");

then do your java code like this,

  ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  Bitmap bit = BitmapFactory.decodeFile("your image path here");
  bit.compress(CompressFormat.PNG, 0, outputStream);

  SQLiteDatabase db = dbh.getWritableDatabase();
  ContentValues cv = new ContentValues();
  ..
 cv.put("images", outputStream.toByteArray());
 ..
 ..
db.insert("tablename", null, cv);
db.close();

this will store your image to db and,

byte []temp = c.getBlob(3);
Bitmap image = BitmapFactory.decodeByteArray(temp, 0, temp.length);
BitmapDrawable dd = new BitmapDrawable(image.getImage());
imgView.setBackgroundDrawable(dd);

this will retrive image from db..

kalandar
  • 793
  • 6
  • 13
  • hii can you tell me whats the "dbh" SQLiteDatabase db = dbh.getWritableDatabase(); in this line.i can't understand..plz help its really help full but i can't understand only this – Google Nov 06 '12 at 11:04
  • dbh is nothing but the object of SQLiteOpenHelper. i hope you have already extends that class to your application for creating database and tables.. – kalandar Nov 07 '12 at 07:30
  • ohkk i understand..and yes i had extended that class – Google Nov 07 '12 at 08:43
  • @kalandar:Hello , maybe you can help me with this? http://stackoverflow.com/questions/15954896/how-to-save-image-taken-from-camera-and-show-it-to-listview-crashes-with-ille .Thanks – George Apr 24 '13 at 09:51
2

OR, you save your image on the phone memory(on the SD Card), or Application cache etc.. and you store the path to retrieve your image file in your SQL database.

VinceFR
  • 2,551
  • 1
  • 21
  • 27
  • I already tried that but i ended up with the problem that you cannot convert a bitmap you set in an image view back to a string (the image path). – user1420042 Jun 04 '12 at 09:57
  • 1
    I'm not sure to understand what you mean.. It's up to you to choose your image path (with getExternalCacheDir()+filename), save it with the Bitmap.compress function, and attach it to your imageView with the ImageView. setImageBitmap function – VinceFR Jun 04 '12 at 10:13
2

Not a good idea.. You should save the image on the disk to reduce sql operations and to improve application performance.. Saving image in DB will make the database heavy in each sense.. Just store image on disk at desired location and save the path along with image name as string in database table.

manurajhada
  • 5,284
  • 3
  • 24
  • 43