我仍然是Android的新手,仍在试图弄清楚如何将图像保存到SQLite DB。因为我试图通过按钮单击事件调用相机拍摄照片,并在返回时尝试将其作为blob保存到数据库中。我知道这可能不是最好的方式,而且据说我不知道如何做到这一点。我最终需要做的是使用我的应用程序访问相机,允许它拍照,并能够将照片保存在数据库中。到目前为止,我已经看过几个论坛,人们建议将位图返回并将其转换为字节数组并保存。那没关系,但我再次感到茫然,任何帮助都会非常感激。这是我目前使用的代码:
//---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);
}
});
这是onActivityResult()方法:
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);
};
};
这是我的数据库类的插入方法:
//---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;
}
这是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
答案 0 :(得分:2)
您通常不会将二进制数据保存到数据库。它被认为是糟糕的形式,并且由于数据最终存储在磁盘上的方式,可以使对数据库的查询耗时很长。您应该做的是在数据库中存储图像的文件路径,并将图像存储在SD卡上。
无需为您编写大量代码,这是一个高级示例。首先,使用以下sql调用创建表来存储图像:
CREATE TABLE images (_id INTEGER PRIMARY KEY AUTOICREMENT, file_path TEXT, name TEXT);
当需要插入图像时,您必须做两件事。首先,将图像写入SD卡。按照instructions here进行操作。一旦你把它写出来并获得了它的文件名,你就像这样做一个SQL查询:
INSERT INTO images (file_path, name) VALUES ("your file path", "name of image");
每当您确实需要显示图像时,只需从SD卡中读取file_path指定的文件。
答案 1 :(得分:0)
首先,Bitmap似乎没有覆盖默认的toString()
构造函数,因此您无法从..." + picture + "...
获取任何有意义的数据。您应该通过调用picture.getPixels
并将其存储来将图片更改为像素数组。您需要将数据库的图片列更改为BLOB类型。此外,您在insert方法中获得空指针异常,可能表示db
为空。你可以自己从DBAdapter类发布406行吗?
答案 2 :(得分:0)
使用getPixels()从位图中检索像素数据,并将int数组保存为数据库中的blob (虽然我会推荐普通文件)。
JavaOCR Project有Android演示,做类似的事情: http://sourceforge.net/projects/javaocr/