我正在制作一个使用它自己的Camera活动的Android应用程序。然后使用IntentService将图像存储在本地SQLite数据库中。
现在的问题是:在拍摄照片后,IntentService无法启动。当在其他场合使用这个IntentService时,它的工作非常完美,所以我不知道为什么它不起作用......
以下是我的CameraActivity中用于拍照并启动IntentService的代码。
mPicture = getPictureCallback();
mRaw = getRawCallback();
mShutter = getShutterCallback();
mCamera.takePicture(mShutter, mRaw, mPicture);
private PictureCallback getPictureCallback() {
PictureCallback picture = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
Log.d(LOG_TAG, "Picture taken, starting service");
Intent databaseIntent = new Intent(MyCameraActivity.this, DBHandlerService.class);
databaseIntent.putExtra("image", data);
databaseIntent.putExtra("action", "save");
databaseIntent.putExtra(AppConstant.RESULT_RECEIVER, mReceiver);
startService(databaseIntent);
mPreview.refreshCamera(mCamera);
}
};
return picture;
}
onHandleEvent:
@Override
protected void onHandleIntent(Intent intent) {
Log.d(LOG_TAG, "onHandle started");
Database db = getDB();
action = intent.getStringExtra("action");
Bundle resultbundle = new Bundle();
Log.d(LOG_TAG, "onHandleIntent action : " + action);
final ResultReceiver reciever = intent
.getParcelableExtra(AppConstant.RESULT_RECEIVER);
reciever.send(STATUS_RUNNING, Bundle.EMPTY);
//Different actions depending on the "action" string
if(action.equals("xxxxx"){
//perform action
} else {
// inform activity the requested action does not exist. When
// STATUS_ERROR is received they must check if errorcode is not
// null.
resultbundle.putString("errormsg", "No actions undertaken");
resultbundle.putInt("errorcode", 9999);
reciever.send(STATUS_ERROR, resultbundle);
}
}
我还使用此IntentService从数据库中获取所有图片。由于数据库保持为空,因此不会返回任何图像,但会打印每个日志。 如果我尝试保存图像,则会打印IntentService中的日志。
如果需要更多代码,请与我们联系。 提前谢谢!
托马斯