我正在尝试建立一个通过 Intents 使用相机的简单 Android 应用程序。该代码几乎直接来自此处的 Android 文档,但它不适用于我。
相机应用程序在调用 startActivityForResult() 后按预期打开,但在我拍照后它永远不会返回 (?!)。具体来说,在我拍照并选择接受图标后它不会返回(Galaxy Nexus 上的复选标记)。但在我选择取消图标(同一部手机上的“X”)后,它确实会返回。
这是代码(请注意,我正在使用片段,而不是活动):
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_layout, container, false);
final Button btnCamera = (Button) view.findViewById(R.id.cameraid);
View.OnClickListener handler = new View.OnClickListener() {
public void onClick(View v) {
if (v == btnCamera) {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// create a file to save the image
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
imagesFolder.mkdirs();
File image = new File(imagesFolder, "image_001.jpg");
Uri uriSavedImage = Uri.fromFile(image);
// start the image capture Intent
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
}
}
btnCamera.setOnClickListener(handler);
}
和
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
// Image captured and saved to fileUri specified in the Intent
Toast.makeText(getActivity(), "Image saved to:\n" +
data.getData(), Toast.LENGTH_LONG).show();
}
else if (resultCode == Activity.RESULT_CANCELED) {
// User cancelled the image capture
} else {
// Image capture failed, advise user
}
}
我需要改变什么才能使这项工作?谢谢。