Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to store image in sqlite database that is taken from gallery and camera android

I have an application in which user add image by clicking on Imageview and setting image and by taking new picture from camera. I want to store that set image into sqlite database.

**AddkeyEventdetail.java**



  package com.example.kidsfinal;

    import java.util.Calendar;

    import android.app.DatePickerDialog;
    import android.app.Dialog;
    import android.content.Intent;
    import android.database.Cursor;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.net.Uri;
    import android.os.Bundle;
    import android.provider.MediaStore;
    import android.support.v4.app.DialogFragment;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.DatePicker;
    import android.widget.EditText;
    import android.widget.ImageView;
    import android.widget.Spinner;
    import android.widget.TextView;

    import com.example.slidingmenuexample.R;

    public class AddChildHoodEventActivity extends SmartActivity {

        ImageView ivChildPics;
        EditText edtEventDate, edtEventDetails;
        Button btnEventDetailSubmit, btnopenCamera;
        Spinner spinnerSelectChild;
        Bitmap bmp;

        Calendar calendar = Calendar.getInstance();
        int mDay = calendar.get(Calendar.DAY_OF_MONTH), mMonth = calendar
                .get(Calendar.MONTH), mYear = calendar.get(Calendar.YEAR) - 1;

        @Override
        public void onCreate(Bundle arg0) {
            // TODO Auto-generated method stub
            super.onCreate(arg0);
            setContentView(R.layout.addchildhoodevents);
            createDrawer();
            initComponent();
            prepareView();
            setOnListener();
        }

        private void initComponent() {
            // TODO Auto-generated method stub
            ivChildPics = (ImageView) findViewById(R.id.kids_addkeyevent_imageview_childpics);
            edtEventDate = (EditText) findViewById(R.id.kids_addkeyevent_edt_addkeyevent);
            edtEventDetails = (EditText) findViewById(R.id.kids_addkeyevent_edt_eventdetails);
            btnEventDetailSubmit = (Button) findViewById(R.id.kids_addkeyevent_btn_submit);
            btnopenCamera = (Button) findViewById(R.id.kids_addkeyevent_btn_opencamera);
        }

        private void prepareView() {

        }

        private void setOnListener() {

            ivChildPics.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    openGallery();

                }

                private void openGallery() {
                    Intent opengallery = new Intent(Intent.ACTION_GET_CONTENT);
                    opengallery.setType("Image/");
                    startActivityForResult(opengallery, 1);
                }
            });

            btnopenCamera.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    OpenCamera();
                }

                private void OpenCamera() {
                    Intent openCam = new Intent(
                            "android.media.action.IMAGE_CAPTURE");
                    startActivityForResult(openCam, 0);

                }
            });



        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == 0 && resultCode == RESULT_OK) {
                if (data != null) {

                    bmp = (Bitmap) data.getExtras().get("data");

                    ivChildPics.setImageBitmap(bmp); /*
                                                     * this is image view where you
                                                     * want to set image
                                                     */

                    Log.d("camera ---- > ", "" + data.getExtras().get("data"));

                }
            }

            else {
                Uri selectedImage = data.getData();

                String[] filePathColumn = { MediaStore.Images.Media.DATA };
                Cursor cursor = getContentResolver().query(selectedImage,
                        filePathColumn, null, null, null);
                cursor.moveToFirst();
                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String filePath = cursor.getString(columnIndex);
                cursor.close();

                if (bmp != null && !bmp.isRecycled()) {
                    bmp = null;
                }

                bmp = BitmapFactory.decodeFile(filePath);
                ivChildPics.setBackgroundResource(0);
                ivChildPics.setImageBitmap(bmp);
            }

        }

    }

database.java

public void insertAddKeyEvent(byte[] childpic,String eventDate,String eventDetails)

{
    ContentValues addkeyValues=new ContentValues();
    addkeyValues.put("CHILDPICS",childpic);
    addkeyValues.put("EVENTDATE", eventDate);
    addkeyValues.put("EVENTDETAILS", eventDetails);

    parentmaster.insert("ADDKEYEVENT",null,addkeyValues);



}
like image 302
user5111 Avatar asked May 15 '14 10:05

user5111


1 Answers

Convert your Bitmap into a byte[] by the following way

ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 100, baos);
byte[] imageData = baos.toByteArray();

and add the byte[] into a BLOB field in your database.

like image 125
Apoorv Avatar answered Nov 15 '22 00:11

Apoorv