简体   繁体   中英

How to retrieve an image from an ImageView and store it in an Sqlite Database in Android?

I have an ImageView in my activity.xml file. I am setting an Image in it which is retrieved from the server. I want to insert this picture from the ImageView into my Sqlite Database. can anyone explain to me how to do it? My table in Sqlite is as such me ( fbId LONG PRIMARY KEY,fbname TEXT,fbuserid INTEGER,fbpic BLOB,ph TEXT,email TEXT) Please tell me step by step as I am new to Databases.
I ahve added the following code bUt an error on insert ie insert is undefined

 Bitmap photo = ((BitmapDrawable)v.getDrawable()).getBitmap();
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    photo.compress(Bitmap.CompressFormat.PNG, 100, bos);
    byte[] bArray = bos.toByteArray();
    Databasehandler db =new Databasehandler(this);    
    ContentValues values = new ContentValues();         
    values.put("image", bArray);            
    db.insert("image" , null, values);

Get bitmap of the image view using:

Bitmap photo = ((BitmapDrawable)image.getDrawable()).getBitmap();

Create byte array of the bitmap:

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

Then you can save data in table using following way.

db = YourDBHelper.getInstance(ctx).getWritableDatabase();    
ContentValues values = new ContentValues();         
values.put("image", bArray);            
db.insert(TABLE_NAME , null, values);

SQLITE data base cannot be used to store Image files directly. However the options you have are following:

  1. Save the image that you received to SD card. May be create a separate folder in which you will store all the images. Then Create a column imagePath in your table structure and save the path of the image to this column.

    Rendering: Here you will fetch the corresponding path from the DB, check if the file Exists at that path(might have been deleted by the user), and if found, get Stream from the file write it to a bitmap and associate it to the required container.

  2. Convert the bitmap received to a BASE64 String. Store this as String to the Data Base.

    Rendering: Here you will have to convert the BASE64 back to Bitmap and associate it to the required container.

Save it on sdcard and path of sdcard on sqlite database

   Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap2.compress(Bitmap.CompressFormat.JPEG, 100,stream);
    File file = new File(path);
    FileOutputStream fOut = new FileOutputStream(file);

    bitmap2.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
    fOut.flush();
    fOut.close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM