java构建一个摄像头应用程序
我是Android编程新手,我正在用Java编写一个应用程序,打开相机拍照并保存。我是通过意图完成的,但我看不到onActivityResult
正在运行
我已经在我的手机(三星Galaxy S)上测试了它,当我拍摄照片时,我会收到照片的预览,其中有两个按钮,一个是保存,另一个是取消。我还没有在我的代码中添加一些东西来实现这一点,所以我认为这是相机的功能。我想在捕获图像后运行onActivityResult
(在我按下预览上的保存按钮后)
但是,在按下预览中的Save按钮后,我将如何返回结果以开始onActivityResult
我忘了告诉你,在我按下save之后,我的整个应用程序都被终止了。 这是我的密码
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TakePicButton = (Button) findViewById(R.id.TakePicture);
TakePicButton.setOnClickListener((安卓.view.View.OnClickListener) this);
}
@Override
public void onDestroy(){
super.onDestroy();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Image captured and saved to fileUri specified in the Intent
Toast.makeText(this, "Image saved to:\n" + data.getData(), Toast.LENGTH_LONG).show();
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT);
} else {
Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT);
}
}
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId() == R.id.TakePicture){
// create Intent to take a picture and return control to the calling application
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
// start the image capture Intent
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
}
共 (0) 个答案