Android的:如何拍照与摄像头和转换位图的字节数组并保存到sqlite的分贝? [英] Android: how to take picture with camera and convert bitmap to byte array and save to sqlite db?

查看:365
本文介绍了Android的:如何拍照与摄像头和转换位图的字节数组并保存到sqlite的分贝?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还是相当新的Andr​​oid和仍在试图弄清楚如何将图像保存到一个SQLite数据库。正如我试图调用摄像头与一个按钮单击事件拍摄照片,而在返回试图将它保存到数据库中的BLOB。我明白,这可能不是最好的办法,而且他这样说,我在如何做到这一点的损失。我ultimaley需要做的是访问的摄像头,支持我的应用程序,以便为它拍照,并能够将照片保存在数据库中。我见过几个论坛,至今人们都建议服用返回的位图,并将其转换为字节数组,只是保存。这将是很好,但我再一次很茫然,任何帮助将大大AP preciated。这里是code我目前使用的:

  // --- pressing此按钮将调用内置的摄像头---
    按钮B4 =(按钮)findViewById(R.id.btn_addPhotos);
    b4.setOnClickListener(新OnClickListener()
    {

        公共无效的onClick(查看为arg0){

            意图cameraIntent =新的意图(android.provider.MediaStore.ACTION_IM​​AGE_CAPTURE);

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

    });
 

这里是onActivityResult()方法:

 保护无效onActivityResult(INT申请code,INT结果code,意图数据){
   如果(结果code == Activity.RESULT_CANCELED)
   {
       //处理当相机被取消......
       Toast.makeText(这一点,相机是由用户取消了......,Toast.LENGTH_LONG).show();

   }否则,如果(结果code == Activity.RESULT_OK)
   {

       。位图imageReturned =(位图)data.getExtras()获得(数据);

       db.open();
       布尔ID = db.insertImage(AssessmentID,imageReturned,测试);
               db.close();

       //显示的消息,如果成功的话...
       displayMessage(ID);

   };
};
 

下面是数据库类的我的插入方法:

  // ---图像插入到数据库---
    公共布尔insertImage(字符串AssessmentID,位图图像,字符串名称)//子字符串,字符串包裹,很多字符串,字符串抬高,字符串数据)
    {
        布尔NUM = FALSE;
        尝试
        {
                db.execSQL(插入storedImages值(+ AssessmentID +,+画面+,+名称+););
                NUM =真;
                //获得空指针试图插入的时候....
        }赶上(的SQLException E)
        {
            e.printStackTrace();
            NUM = FALSE;
        };

            //返回true,如果成功,否则为false ...
        返回NUM;
    }
 

这里是logcat的错误:

  10-23 15:10:51.890:ERROR / NvOmxCameraSettingsparser(89):参数类型13不支持
10-23 15:10:51.890:ERROR / NvOmxCameraSettingsparser(89):参数类型49不支持
不支持32:10-23 15:10:52.200:ERROR / ImagerODM-OV5650(89):通过setParameter()
10-23 15:10:57.510:ERROR / NvOmxCamera(89):已经调用发布()
10-23 15:11:08.020:ERROR / AndroidRuntime(10123):致命异常:主要
10-23 15:11:08.020:ERROR / AndroidRuntime(10123):java.lang.RuntimeException的:不提供结果ResultInfo {谁= NULL,请求= 1337,结果= -1,数据=意向{行为=内联数据(有群众演员)}}到活动{king.chad.SDE / king.chad.SDE.NewResidentialActivity}:显示java.lang.NullPointerException
10-23 15:11:08.020:ERROR / AndroidRuntime(10123):在android.app.ActivityThread.deliverResults(ActivityThread.java:2818)
10-23 15:11:08.020:ERROR / AndroidRuntime(10123):在android.app.ActivityThread.handleSendResult(ActivityThread.java:2861)
10-23 15:11:08.020:ERROR / AndroidRuntime(10123):在android.app.ActivityThread.access $ 1000(ActivityThread.java:122)
10-23 15:11:08.020:ERROR / AndroidRuntime(10123):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1054)
10-23 15:11:08.020:ERROR / AndroidRuntime(10123):在android.os.Handler.dispatchMessage(Handler.java:99)
10-23 15:11:08.020:ERROR / AndroidRuntime(10123):在android.os.Looper.loop(Looper.java:132)
10-23 15:11:08.020:ERROR / AndroidRuntime(10123):在android.app.ActivityThread.main(ActivityThread.java:4123)
10-23 15:11:08.020:ERROR / AndroidRuntime(10123):在java.lang.reflect.Method.invokeNative(本机方法)
10-23 15:11:08.020:ERROR / AndroidRuntime(10123):在java.lang.reflect.Method.invoke(Method.java:491)
10-23 15:11:08.020:ERROR / AndroidRuntime(10123):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:841)
10-23 15:11:08.020:ERROR / AndroidRuntime(10123):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
10-23 15:11:08.020:ERROR / AndroidRuntime(10123):在dalvik.system.NativeStart.main(本机方法)
10-23 15:11:08.020:ERROR / AndroidRuntime(10123):由:显示java.lang.NullPointerException
10-23 15:11:08.020:ERROR / AndroidRuntime(10123):在king.chad.SDE.DBAdapter.insertImage(DBAdapter.java:406)
10-23 15:11:08.020:ERROR / AndroidRuntime(10123):在king.chad.SDE.NewResidentialActivity.onActivityResult(NewResidentialActivity.java:168)
10-23 15:11:08.020:ERROR / AndroidRuntime(10123):在android.app.Activity.dispatchActivityResult(Activity.java:4581)
10-23 15:11:08.020:ERROR / AndroidRuntime(10123):在android.app.ActivityThread.deliverResults(ActivityThread.java:2814)
10-23 15:11:08.020:ERROR / AndroidRuntime(10123):11 ...更多
 

解决方案

您一般不二进制数据保存到数据库中。它被认为不好的形式,可以使数据库查询取的,因为数据就结束了存储在磁盘上的路很长。你应该做的是一个文件路径存储在数据库中的图像和图像存储在SD卡。

不用写code大量对你来说,这里有一个高层次的例子。首先,创建表来存储图像用下面的SQL调用:

  CREATE TABLE图像(_id INTEGER PRIMARY KEY AUTOICREMENT,FILE_PATH TEXT,名称文本);
 

当谈到时间插入你必须做两件事情的图像。首先,写出图像到SD卡。按照这里说明做到这一点。一旦你已经写了出来,并为它得到一个文件名,你只是做一个SQL查询,如下所示:

  INSERT INTO图像(形象的名字你的文件路径,)(FILE_PATH,名)价值;
 

当你真正需要显示的图像,你只需从你的SD卡读取FILE_PATH指定的文件。

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

解决方案

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.

这篇关于Android的:如何拍照与摄像头和转换位图的字节数组并保存到sqlite的分贝?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆