-1

I am storing an image taken from camera into sqlite database can anybody help me to retrieve the same image and i want to show that image in a image view.

Here is my Database handler class method for saving the image

public long insert(Bitmap img ) {

        SQLiteDatabase base=this.getWritableDatabase();
        byte[] data = getBitmapAsByteArray(img); // this is a function
        ContentValues value=new ContentValues();
        value.put("image",data);

        long a= base.insert("Mypic", null, value); 
        System.out.println("check1" + a);

        return a; 
    }


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

Please help me for writing the method how i can get the image and display the image in an imageview.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Sumeet Rathore
  • 232
  • 3
  • 16
  • 4
    Don't store the image in the database, just store it in the data folder. You're making it harder than it is. – se_bastiaan Feb 20 '15 at 13:41
  • 1
    Sqlite is a lite implementation of SQL, they have primitve type of data types like text and number .. , they do not support BLOB types. It is better to store physical file on your sdcard in "dot" folders and save their paths in your db table. – Murtaza Khursheed Hussain Feb 20 '15 at 13:43
  • @se_bastiaan thanx for the suggestion sir but could you please tell me the disadvantage why i shouldn't store the image in the database and also please give any helpful link or code to store the image in data folder. – Sumeet Rathore Feb 20 '15 at 13:46
  • @Murtaza Hussain Sir could you please provide me any helpful link to get the functionality you have told or guide me with some helpful steps. – Sumeet Rathore Feb 20 '15 at 13:49
  • 1
    `tell me the disadvantage`. Well if you read Hussan's comment carefully you will know that is not a matter of disadvantage but of impossibility. – greenapps Feb 20 '15 at 13:49
  • @SumeetRathore : follow this step by step ,and hit up if useful for you ..http://stackoverflow.com/questions/7331310/how-to-store-image-as-blob-in-sqlite-how-to-retrieve-it – Radhey Feb 20 '15 at 14:17
  • anything above 1MB will crash the DB. That's why images are not suitable to be included in DB. And to save to disk is just compress the bitmap (like you're already doing into a FileOutputStream: https://developer.android.com/reference/java/io/FileOutputStream.html – Budius Feb 20 '15 at 15:19

1 Answers1

2

To insert Image on database:

    Bitmap bitmap = ((BitmapDrawable) image_imgv.getDrawable()).getBitmap();

    ByteArrayOutputStream bos4 = new ByteArrayOutputStream();
    bitmap4.compress(Bitmap.CompressFormat.PNG, 100, bos4);
    image = bos4.toByteArray();

    database = new BBDD(this, "BBDD", null, 1);
    SQLiteDatabase db = database.getWritableDatabase();

    ContentValues reg = new ContentValues();
    reg.put("img", image);

To retrieve:

database2 = new BBDD(Activity.this, "BBDD", null, 1);
            SQLiteDatabase db2 = database2.getReadableDatabase();

            if (db2 != null)
            {
                Cursor cursor = db2.rawQuery("SELECT img FROM database2, null);
                if (cursor.moveToFirst())
                {
                    img=cursor.getBlob(cursor.getColumnIndex("img"));
                    Bitmap b1=BitmapFactory.decodeByteArray(image, 0, image.length);
                    image_imageview.setImageBitmap(b1);

                }
                else
                    Toast.makeText(Activity.this, "Error.", Toast.LENGTH_LONG).show();

                db2.close();
            }
            else
                Toast.makeText(sActivity.this, "Error db.", Toast.LENGTH_LONG).show();
        }
    });
Aspicas
  • 4,498
  • 4
  • 30
  • 53