Android Save Images to SQLite or Sdcard or Memory

Android Save images to SQLite or SDCard or memory

Always make a habit of saving images path to database. For a list view, be sure just to use those image's thumbnail. This will help you in fast loading of these images in list.

 long selectedImageUri = ContentUris.parseId(Uri.parse(anniEntry.getUri()));
Bitmap bm = MediaStore.Images.Thumbnails.getThumbnail(
mContext.getContentResolver(), selectedImageUri,MediaStore.Images.Thumbnails.MICRO_KIND,
null );

Here anniEntry.getUri() is the image uri.Now,put it in second code.U can get micro or mini thumbnail according to requirement

How to store image in SQLite database

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;
}

How to select an image from internal storage and insert it in SQLite?

Alright, as Lingviston already pointed out. You can implement picking an image from the gallery from here.

As for storing the image, I'm going to edit the code in the link a little bit.

Instead of this:

ImageView imageView = (ImageView) findViewById(R.id.imgView);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

I'm going to store the selected image in a Bitmap.

ImageView imageView = (ImageView) findViewById(R.id.imgView);
Bitmap mBitmap = BitmapFactory.decodeFile(picturePath);
imageView.setImageBitmap(mBitmap);

Now to store in as a BLOB type in android you need to convert the bitmap into bytes and then store the byte array in the sqlite database.

                ByteArrayOutputStream stream = new ByteArrayOutputStream();
mBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] imageInByte = stream.toByteArray();

Now you just need to pass imageInByte to store in the SQLite database.

As a side note, both of these answers were already available all over the internet and StackOverflow individually, you just had to put 2 and 2 together. Please try searching thoroughly before you post a question.

how can I store image and retrieve the same in sqlite-android

sqlite3 supports the blob type, you can save the bitmap content using the blob type.

However, the blob type has a size limit, and to save to a blob type is difficult.

So, I suggest to save the bitmap local or on the sdcard, and save the path in the database.

added :

table define a column with name 'image' using blob type

    Bitmap map = ...;
ByteArrayOutputStream bufferStream = new ByteArrayOutputStream(16*1024);
map.compress(CompressFormat.JPEG, 80, bufferStream);
byte[] bytes = bufferStream.toByteArray();
ContentValues values = new ContentValues();
values.put("image", bytes);

convert the image byte array, using SQLiteDatabase class method or content provider as you like:

public long insert (String table, String nullColumnHack, ContentValues values)

to insert to table, so it save the image to blob.

and then: when you query the blob data, you can create the image like this:

        BitmapFactory.Options option2 = new BitmapFactory.Options();
option2.inPreferredConfig = Bitmap.Config.RGB_565;
// added for reducing the memory
option2.inDither = false;
option2.inPurgeable = true;
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length, option2);

hope it can implement your request. -):

Store Android SQLite

Following Answer I am giving on the based on my personal experience.

  • First Suggestion ( Internal/Phone Memory )

    When you are storing database in Internal Memory, then for the Performance it is very good. Because your Application will be installed in the Internal Memory, so while inserting 1000 Records and other record ,for the application it would be more easier to access the database. Application's performance will be very nice. But when it comes to Memory limitations, when Internal Memory will get filled more than enough then your device's performance will be down. So Internal Memory is best till medium level database.

  • Second Suggestion ( External Memory, SD Card )

    When you are storing database in external Memory like SD Card, then for performance , application performance’s will be bit slower than Internal Memory. However this won't be significant for medium level database. When there is very few data then performance will be same in both the cases. Now when you are using External Memory, then you have large size of data storing. I do not think it will never get filled fully . But in case of large number of records performance will be down. Sometimes it may be possible that application crashes.



Related Topics



Leave a reply



Submit