Search code examples
androidsqlitebitmapcamerasave-image

Android: how to take picture with camera and convert bitmap to byte array and save to sqlite db?


I'm still fairly new to android and still trying to figure out how to save an image to a SQLite DB. As is I am trying to call the camera with a button click event to take the photo and on the return trying to save it to a database as a blob. I understand that this may not be the best way, and with that being said I'm at a loss of how to do this. What I ultimaley need to do is to access the camera with my app, allow for it to take a photo, and to be able to save the photo in a data base. I've seen a few forums so far where people have suggested taking the bitmap returned and convert it to a byte array and just save that. That would be fine but once again I am at a loss, any help would be greatly appreciated. Here is the code i am currently using:

//---Pressing this button will call the built in Camera---
    Button b4 = (Button) findViewById(R.id.btn_addPhotos);
    b4.setOnClickListener(new OnClickListener()
    {

        public void onClick(View arg0){

            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);  

            //startActivity(cameraIntent);  
            startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);  
        }

    });

here is the onActivityResult() method:

protected void onActivityResult(int requestCode, int resultCode, Intent data){
   if (resultCode == Activity.RESULT_CANCELED)
   {
       //handles when camera was canceled...
       Toast.makeText(this, "Camera was cancelled by user...", Toast.LENGTH_LONG).show();

   } else if (resultCode == Activity.RESULT_OK)
   {

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

       db.open();
       boolean id = db.insertImage(AssessmentID, imageReturned, "test");
               db.close();

       //displays message, if successful...
       displayMessage(id); 

   };
};

Here is my insert method of the database class:

 //---insert image into the database---
    public boolean insertImage(String AssessmentID, Bitmap picture, String name)//, String sub, String parcel, String lot, String elevation, String datum) 
    {
        boolean num = false;
        try
        {
                db.execSQL("insert into storedImages values(" + AssessmentID + ", " + picture + ", " + name + ");");
                num = true;
                //getting null pointer when trying to insert....
        }catch(SQLException e)
        {
            e.printStackTrace();
            num = false;
        };

            //returns true if successful and false if not...
        return num;
    }

here is error of the logcat:

10-23 15:10:51.890: ERROR/NvOmxCameraSettingsparser(89): Param type 13 not supported
10-23 15:10:51.890: ERROR/NvOmxCameraSettingsparser(89): Param type 49 not supported
10-23 15:10:52.200: ERROR/ImagerODM-OV5650(89): SetParameter(): 32 not supported
10-23 15:10:57.510: ERROR/NvOmxCamera(89): Already called release()
10-23 15:11:08.020: ERROR/AndroidRuntime(10123): FATAL EXCEPTION: main
10-23 15:11:08.020: ERROR/AndroidRuntime(10123): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1337, result=-1, data=Intent { act=inline-data (has extras) }} to activity {king.chad.SDE/king.chad.SDE.NewResidentialActivity}: java.lang.NullPointerException
10-23 15:11:08.020: ERROR/AndroidRuntime(10123):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2818)
10-23 15:11:08.020: ERROR/AndroidRuntime(10123):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:2861)
10-23 15:11:08.020: ERROR/AndroidRuntime(10123):     at android.app.ActivityThread.access$1000(ActivityThread.java:122)
10-23 15:11:08.020: ERROR/AndroidRuntime(10123):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1054)
10-23 15:11:08.020: ERROR/AndroidRuntime(10123):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-23 15:11:08.020: ERROR/AndroidRuntime(10123):     at android.os.Looper.loop(Looper.java:132)
10-23 15:11:08.020: ERROR/AndroidRuntime(10123):     at android.app.ActivityThread.main(ActivityThread.java:4123)
10-23 15:11:08.020: ERROR/AndroidRuntime(10123):     at java.lang.reflect.Method.invokeNative(Native Method)
10-23 15:11:08.020: ERROR/AndroidRuntime(10123):     at java.lang.reflect.Method.invoke(Method.java:491)
10-23 15:11:08.020: ERROR/AndroidRuntime(10123):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
10-23 15:11:08.020: ERROR/AndroidRuntime(10123):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
10-23 15:11:08.020: ERROR/AndroidRuntime(10123):     at dalvik.system.NativeStart.main(Native Method)
10-23 15:11:08.020: ERROR/AndroidRuntime(10123): Caused by: java.lang.NullPointerException
10-23 15:11:08.020: ERROR/AndroidRuntime(10123):     at king.chad.SDE.DBAdapter.insertImage(DBAdapter.java:406)
10-23 15:11:08.020: ERROR/AndroidRuntime(10123):     at king.chad.SDE.NewResidentialActivity.onActivityResult(NewResidentialActivity.java:168)
10-23 15:11:08.020: ERROR/AndroidRuntime(10123):     at android.app.Activity.dispatchActivityResult(Activity.java:4581)
10-23 15:11:08.020: ERROR/AndroidRuntime(10123):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2814)
10-23 15:11:08.020: ERROR/AndroidRuntime(10123):     ... 11 more

Solution

  • You generally don't save binary data to a database. It's considered bad form and can make queries to the database take really long because of the way the data ends up getting stored on the disk. What you should do is store a file path to the image in your database and store the image on the sdcard.

    Without writing massive amounts of code for you, here's a high-level example. First, create your table to store the image with the following sql call:

    CREATE TABLE images (_id INTEGER PRIMARY KEY AUTOICREMENT, file_path TEXT, name TEXT);
    

    When it comes time to insert an image you have to do two things. First, write the image out to the SD card. Follow the instructions here to do that. Once you've written it out and gotten a file name for it, you just do a sql query like so:

    INSERT INTO images (file_path, name) VALUES ("your file path", "name of image");
    

    Whenever you actually need to display the image, you just read the file specified by file_path from your sdcard.