我对 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