0

At this time, I have some case in developing android application, right now I have some class called DBHelper, this class is use to execute operation like create table and operation like create trigerrs and so on.. In this case I tried to execute sql statement for inserting data in my table, but the sql operation is not working, can anybody help me? I really need help here.

Here's my class..

 public class DBHelper extends SQLiteOpenHelper
 {
 private Context mContext;
 private static final String db_name ="schoolmap.db";
 private static final int db_version=1;

   //Constructor
   public DBHelper(Context context) {
        super(context, db_name, null, db_version);
        this.mContext = context;

    }
 private static final String db_TABLE_foto = "create table "
          + "foto" + "("
          +  id  + " integer primary key autoincrement, "
          + caption_foto + " varchar(20), "
          + image + " BLOB);";

@Override
public void onCreate(SQLiteDatabase db) {

db.execSQL(db_TABLE_foto);

//in this line i tried to add picture from the drawable folder
Bitmap b = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.a1);
    int bytes = b.getByteCount();
    ByteBuffer buffer = ByteBuffer.allocate(bytes); //Create a new buffer
    b.copyPixelsToBuffer(buffer);
    byte[] array = buffer.array();

//the question is right here, when I try this code, it's not working, means: the record is not 
//inserted 
ContentValues values = new ContentValues();
    values.put(caption_foto, "Test"); 
    values.put(image, array); 

    // Inserting Row
    database.insert(foto, null, values);
}

 @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.w(DBHelper.class.getName(),"Upgrading database from version " + oldVersion + "to"
            + newVersion + ",which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + db_TABLE_foto);
onCreate(db);

}

so I mean, when I running this apps first time, I tried to add picture in my application, but the sql operation is not working, can somebody help about this issue? Any Help is Needed

user3747208
  • 21
  • 1
  • 6

2 Answers2

1

In common case it's not good idea to put images into database. You can fast and easy access to image from resources without using db. Look answer here

The best way is to save image to file system and save uri of it to string field in database. If you need to link record in database to image in resources, you can save resource id (like R.drawable.a1) in int field.

Community
  • 1
  • 1
Alexander Mikhaylov
  • 1,790
  • 1
  • 13
  • 23
  • so, you think i must save the path of images from the internet? – user3747208 Jun 19 '14 at 09:10
  • If you have images in the net and want to work offline. You must save download files to disk and save in your directory on the sd card. After that, save path to image on disk to your database. If you want to work only online, you can save url to your database. – Alexander Mikhaylov Jun 19 '14 at 09:13
  • @user3747208 also, you can save url to the internet in your database and use Picasso to load it in your ImageViews. Picasso will also controll image cache, resizing and some more. http://square.github.io/picasso/ – Alexander Mikhaylov Jun 19 '14 at 09:15
1
    Try This code:-

    private ByteArrayOutputStream a_thumbnail = new ByteArrayOutputStream();
    Bitmap b = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.a1);
    b.compress(Bitmap.CompressFormat.PNG, 80, a_thumbnail);

    ContentValues values = new ContentValues();
        values.put("Test",caption_foto,); 
        values.put("Your image column name", a_thumbnail.toByteArray());   
database.insert("foto", null, values);
Rutvij
  • 493
  • 3
  • 10
  • Yes, this code should work for small pictures, but in common case it's bad to use blob on android. Look at my answer. – Alexander Mikhaylov Jun 19 '14 at 08:59
  • @Rutvij : i already did that, but the code is not working.. it's still give an erorr, Log cat say Failed to read row 0, from a CursorWindow which has 0 rows, 2 Columns.. – user3747208 Jun 19 '14 at 09:08
  • @user3747208 "Failed to read (...) from a CursorWindow" has nothing to do with your question... – sergio91pt Jun 19 '14 at 09:40
  • @Rutvij : when i your code, there's nothing error.. but when i tried to see the data, the the log it's show what i comment before.. – user3747208 Jun 19 '14 at 09:43
  • @sergio91pt : that's right, when i tried my code or Rutvij code, it's doing fine, but when i checked into my data, then data won't show up – user3747208 Jun 19 '14 at 09:46