0

i have this class, where i create my tables in sqlite, everthing is working so good, in my project you can insert,delete and update (the info) of students and teachers, but now i want save an image, it means that apart of save the info of the student or teacher i want to save an image, please can someone helps me? y dont know anythig about how to do that.

package MyClasses;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class sqliteClass extends SQLiteOpenHelper{

String sqlAlumno = "CREATE TABLE Alumno(id integer primary key,nombre varchar(30),apellidos varchar(25),especialidad varchar(25),email varchar(25))";

String sqlProfe = "CREATE TABLE Profe(id integer primary key,nombre varchar(30),apellidos varchar(25),especialidad varchar(25),email varchar(25))";

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

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(sqlAlumno);
        db.execSQL(sqlProfe);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {


    }

}
ArmandoCl
  • 9
  • 1
  • 2

3 Answers3

1

In general terms :

add a column with type blob ,say image blob, into your create table query

to insert an image :

ContentValues cv = new ContentValues();
ByteArrayOutputStream out = new ByteArrayOutputStream(); 
yourBitmap.getBitmap().compress(Bitmap.CompressFormat.PNG, 100, out);
cv.put(KEY_IMG, out.toByteArray());

to restore an image:
fetch the images using Cursor#getBlob() method and store it in a byte[] array

byte[] blob = cursor.getBlob(cur.getColumnIndex("image"));

convert byte[] into bitmap :

Bitmap bmp = BitmapFactory.decodeByteArray(blob, 0, blob.length);

However I think in big level storing images in database is not valuable because sqlite have limited space so storing the image path is valuable.

Mohsen Kamrani
  • 7,177
  • 5
  • 42
  • 66
0

There are several ways to store an images to android sqlite database.

stackoverflow.com/questions/10977584

stackoverflow.com/questions/9357668

stackoverflow.com/questions/9273008

hope these help!

Community
  • 1
  • 1
0

It's generally considered bad form to save binary data like an image in a database. In most cases, for some technical reasons, it will actually end up damaging the performance of your database. Instead, save the images you want to cache to the SD card and just store the filepath of those images in the database.

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

kunal
  • 308
  • 3
  • 15