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

断了今生、忘了曾经 提交于 2019-12-05 06:53:54

问题


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

回答1:


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.




回答2:


Use getPixels() to retrieve pixel data from bitmap and save int array as a blob in database ( though I would recommend plain file ).

JavaOCR Project has android demos, doing something similar: http://sourceforge.net/projects/javaocr/




回答3:


First of all, it appears that Bitmap does not override the default toString() constructor, so you cannot get any meaningful data from ..." + picture + ".... You should look into changing the picture into an array of pixels, by calling picture.getPixels and storing that instead. You will need to change the picture column of your database to a BLOB type. Also, you are getting a null pointer exception in your insert method, possibly indicating that db is null. Could you post line 406 from your DBAdapter class by itself?



来源:https://stackoverflow.com/questions/7868455/android-how-to-take-picture-with-camera-and-convert-bitmap-to-byte-array-and-sa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!