0

I want to insert the image, delete the images and retrieving all the images from SQLite database. I am creating the database with code shown below.

But how can I call these methods in MainActivity?

public class Imagehelper extends SQLiteOpenHelper {     
        private static final String DATABASE_NAME="abhi.db";
        private static final int SCHEMA_VERSION=1; 

        public Imagehelper(Context context) {
            super(context, DATABASE_NAME, null, SCHEMA_VERSION);
            }
        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub          
            db.execSQL("CREATE TABLE Image(_id INTEGER PRIMARY KEY AUTOINCREMENT,imageblob BLOB);");
            }
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        }       
        public Cursor getAll() {                
            return(getReadableDatabase().rawQuery("SELECT imageblob FROM Image",null));
        }   
        public void insert(byte[] bytes)
        {
            ContentValues cv=new ContentValues();

            cv.put("imageblob",bytes);
            Log.e("inserted", "inserted");
            getWritableDatabase().insert("Image","imageblob",cv);
            System.out.println(":vlfkv: " +cv.size());
        }
        public byte[] getImage(Cursor c)
          {
              return(c.getBlob(1));
          }

        public void deletePhoto(String id) {

            SQLiteDatabase database = this.getWritableDatabase();    
            String deleteQuery = "DELETE FROM  Image where _id='"+ id +"'";
            Log.d("query",deleteQuery);     
            database.execSQL(deleteQuery);
        }
    }
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user3114723
  • 401
  • 1
  • 6
  • 17
  • Please look at http://stackoverflow.com/questions/1636877/how-can-i-store-and-retrieve-images-from-a-mysql-database-using-php link –  Dec 27 '13 at 09:11
  • 2
    So what is the problem? – Piyush Dec 27 '13 at 09:13
  • Please go for this tutorial, it will help you alot in the long run: http://developer.android.com/training/notepad/index.html – Skynet Dec 27 '13 at 09:15
  • Make **Imagehelper** object and initialize it and access your method which you want in your Activity.. – Piyush Dec 27 '13 at 09:17
  • You should storage image path and use anywhere. [Check one simple Local database example here](http://chintankhetiya.wordpress.com/2013/06/01/sqlite-database-example/) – Chintan Khetiya Dec 27 '13 at 09:18

3 Answers3

0

You need to have an instance of your Imagehelper in your Activity :

Imagehelper mImageHelper = new Imagehelper();

// and then you can access your methods ...
mImageHelper.insert(yourValue);
mImageHelper.get(yourValue);
mImageHelper.deletePhoto(yourValue);

I suggest you follow some dabatase tutorials for Android :

http://www.vogella.com/articles/AndroidSQLite/article.html

http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/

Andros
  • 4,069
  • 1
  • 22
  • 30
0

use this code. where image is byte array. Getting all images as byte[] code u have already written

Bitmap bmp = BitmapFactory.decodeByteArray(image, 0, image.length);
Chintan Khetiya
  • 15,962
  • 9
  • 47
  • 85
Sush
  • 3,864
  • 2
  • 17
  • 35
0

you have not call select query in getImage method. so see below function.

public byte[] getImage(
{
   Cursor c = database.rawQuery("SELECT imageblob FROM Image",null);               
   return(c.getBlob(1));
}
Sanket Kachhela
  • 10,861
  • 8
  • 50
  • 75