abirami bhargavi
abirami bhargavi

Reputation: 49

Pick an image from gallery and store and retrieve it in sqlite database

I am new to Android and I have knowledge on sqlite in Android. I know how to retrieve image from gallery. Can anyone please help me to store that retrieved image in SQL and also to retrieve?

And I have tried to store and retrieve. I share my code here:

My Database Handler class:

public class DatabaseHandler extends SQLiteOpenHelper
{
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "image";
    private static final String TABLE_NAME = "store";
    private static final String COLUMN_IMAGE = "img";
    private static final String COLUMN_ID = "id";
    private static final String CREATE_TABLE_IMAGE =
            "CREATE TABLE " + TABLE_NAME + " (" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +COLUMN_IMAGE + " BLOB NOT NULL " + ")";

    public DatabaseHandler(Context context)
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db)
    {
        db.execSQL(CREATE_TABLE_IMAGE);
        Log.i("Table..","Created");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
        db.execSQL("DROP TABLE IF EXISTS " + CREATE_TABLE_IMAGE);
        onCreate(db);
    }

    public void insertBitmap(Bitmap bmp)
    {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.PNG, 100, out);
        byte[] buffer=out.toByteArray();

        SQLiteDatabase db = this.getWritableDatabase();

        db.beginTransaction();
        ContentValues values;

        try
        {
            values = new ContentValues();
            values.put("img",buffer);

            db.insert(TABLE_NAME, null, values);
            db.setTransactionSuccessful();
            Log.i("Image..","Inserted..");
        }
        catch (SQLiteException e)
        {
            e.printStackTrace();

        }
        finally
        {
            db.endTransaction();
            // End the transaction.
            db.close();
            // Close database
        }
    }

    public Bitmap getBitmap(int id)
    {
        Bitmap bitmap = null;
        SQLiteDatabase db = this.getReadableDatabase();

        db.beginTransaction();
        try
        {
            String selectQuery = "SELECT * FROM "+ TABLE_NAME+" WHERE id = " + id;
            Cursor cursor = db.rawQuery(selectQuery, null);

            int id_value = cursor.getInt(0);
            POJO pojo = new POJO(id_value);
            Log.i("AUTO ID..",cursor.getString(0));

            if(cursor.getCount() >0)
            {
                while (cursor.moveToNext())
                {
                    byte[] blob = cursor.getBlob(cursor.getColumnIndex(COLUMN_IMAGE));
                    bitmap= BitmapFactory.decodeByteArray(blob, 0, blob.length);
                }

            }
            db.setTransactionSuccessful();
        }
        catch (SQLiteException e)
        {
            e.printStackTrace();

        }
        finally
        {
            db.endTransaction();
            // End the transaction.
            db.close();
            // Close database
        }
        return bitmap;
    }

}

My Activity class:

public class MainActivity extends AppCompatActivity
{
    DatabaseHandler dbh;
    POJO pojo;
    ImageView imageView;
    TextView textView;
    private static int RESULT_LOAD_IMG = 1;
    String picturePath;
    int id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        imageView = (ImageView) findViewById(R.id.image);
        textView = (TextView)findViewById(R.id.textView);

        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(i, RESULT_LOAD_IMG);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

      try
       {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
            {
                if (!Settings.System.canWrite(this) && (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK && null != data))
                {
                    requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
                            Manifest.permission.READ_EXTERNAL_STORAGE}, 2909);

                    Uri selectedImage = data.getData();
                    String[] filePathColumn = {MediaStore.Images.Media.DATA};

                    Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);

                    assert cursor != null;
                    cursor.moveToFirst();

                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    picturePath = cursor.getString(columnIndex);
                    cursor.close();
                    Bitmap bm = BitmapFactory.decodeFile(picturePath);
                    Log.i("Hii...", "After Cursor..");
                    getImage(bm);

                }
            }


        {
            Toast.makeText(getApplicationContext(), "Something went wrong", Toast.LENGTH_LONG).show();
        }
    }

    public void getImage(Bitmap bm)
    {

        Log.i("Hi..","In getImage");        int id = pojo.getId();
        dbh.insertBitmap(bm);
        imageView.setImageBitmap(dbh.getBitmap(id));
    }
}

POJO class:

public class POJO {
    int id;

    public POJO (int id1)
    {
        this.id = id1;
    }

    public int getId()
    {
        return id;
    }
}

Upvotes: 2

Views: 6521

Answers (2)

Rahul Sonpaliya
Rahul Sonpaliya

Reputation: 156

I've made some changes in your code and now it is working as expected.

Changed DatabaseHandler class :

public class DatabaseHandler extends SQLiteOpenHelper {
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "image";
    private static final String TABLE_NAME = "store";
    private static final String COLUMN_IMAGE = "img";
    private static final String COLUMN_ID = "id";
    private static final String CREATE_TABLE_IMAGE =
            "CREATE TABLE " + TABLE_NAME + " (" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_IMAGE + " BLOB NOT NULL " + ")";

    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE_IMAGE);
        Log.i("Table..", "Created");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }

    public long insertBitmap(Bitmap bmp) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.PNG, 100, out);
        byte[] buffer = out.toByteArray();

        SQLiteDatabase db = this.getWritableDatabase();

        db.beginTransaction();
        ContentValues values;

        long id = 0;

        try {
            values = new ContentValues();
            values.put("img", buffer);

            id = db.insert(TABLE_NAME, null, values);
            db.setTransactionSuccessful();
            Log.i("Image..", "Inserted..");
        } catch (SQLiteException e) {
            e.printStackTrace();

        } finally {
            db.endTransaction();
            // End the transaction.
            db.close();
            // Close database
        }
        return id;
    }

    public Bitmap getBitmap(int id) {
        Bitmap bitmap = null;
        SQLiteDatabase db = this.getReadableDatabase();

        db.beginTransaction();
        try {
            String selectQuery = "SELECT * FROM " + TABLE_NAME + " WHERE id = " + id;
            Cursor cursor = db.rawQuery(selectQuery, null);

            if (cursor.getCount() > 0) {
                while (cursor.moveToNext()) {
                    byte[] blob = cursor.getBlob(cursor.getColumnIndex(COLUMN_IMAGE));
                    bitmap = BitmapFactory.decodeByteArray(blob, 0, blob.length);
                }

            }
            db.setTransactionSuccessful();
        } catch (SQLiteException e) {
            e.printStackTrace();

        } finally {
            db.endTransaction();
            // End the transaction.
            db.close();
            // Close database
        }
        return bitmap;
    }
}

Changed MainActivity class :

public class MainActivity extends AppCompatActivity
{
    DatabaseHandler dbh;
    POJO pojo;
    ImageView imageView;
    TextView textView;
    private static int RESULT_LOAD_IMG = 1;
    String picturePath;
    int id;
    public final int REQUEST_CODE_FOR_PERMISSIONS = 654;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = (ImageView) findViewById(R.id.image);
        textView = (TextView)findViewById(R.id.textView);

        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(i, RESULT_LOAD_IMG);
            }
        });

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED &&
                    ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(
                        this,
                        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
                                Manifest.permission.READ_EXTERNAL_STORAGE},
                        REQUEST_CODE_FOR_PERMISSIONS);
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == RESULT_LOAD_IMG){
            Uri selectedImage = data.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};

            Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);

            assert cursor != null;
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            picturePath = cursor.getString(columnIndex);
            cursor.close();
            Bitmap bm = BitmapFactory.decodeFile(picturePath);
            Log.i("Hii...", "After Cursor..");
            getImage(bm);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(requestCode == REQUEST_CODE_FOR_PERMISSIONS){
            //You need to handle permission results, if user didn't allow them.
        }
    }

    public void getImage(Bitmap bm) {

        Log.i("Hi..", "In getImage "+bm);
        dbh = new DatabaseHandler(this);
        int id = (int) dbh.insertBitmap(bm);
        imageView.setImageBitmap(dbh.getBitmap(id));
    }
}

I've tested this code on my HTC marshmallow device, it is working fine.

When i first installed this app, it asks me for permissions and i've allowed both permissions.

Upvotes: 1

Hossein Nakhaeipoor
Hossein Nakhaeipoor

Reputation: 144

I suggest you store the URI of your image (as a string) into your database instead of putting bytes directly into the database.

This will be more precise and standard.

UPDATE:

First store the URI of your image into database as an string so you can use it later:

String str = imageUri.toString();
databaseHelper.insert(str); 

and when you want to load the image you can simply decode the URI from your database that you saved in the past:

String str = databaseHelper.retrive(someId);
Uri imageUri = Uri.parse(str);  

Note that imageUri is the address of your image in a wrapper called Uri. It's just a way to present paths and addresses that the Android framework uses.

Upvotes: 1

Related Questions