我正在启动一台相机ActivityForResult
,我resultCode
的返回值为 0,而我requestCode
的返回值为 -1。有趣的是,这在我使用resultCode
. 请允许我向您展示
private class ClickListener implements View.OnClickListener {
@Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA);
}
}
public void onActivityResult(int resultCode, int requestCode, Intent data) {
if (requestCode == CAMERA) {
Bundle extras = data.getExtras();
bmp = (Bitmap) extras.get("data");
MediaStore.Images.Media.insertImage(getActivity()
.getContentResolver(), bmp, "picture", "a picture");
Log.v("HEY", "Your image should be in the gallery now");
}
}
但是我显然需要添加一些代码,以防用户按下取消而不是拍摄照片。所以我添加了一些代码,我的新onActivityResult
外观像这样
public void onActivityResult(int resultCode, int requestCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.v("RESULT CODE", "" + resultCode);
Log.v("REQUEST CODE", "" + requestCode);
if (requestCode == CAMERA) {
if (resultCode == Activity.RESULT_OK) {
Bundle extras = data.getExtras();
bmp = (Bitmap) extras.get("data");
MediaStore.Images.Media.insertImage(getActivity()
.getContentResolver(), bmp, "picture", "a picture");
Log.v("HEY", "Your image should be in the gallery now");
} else if (resultCode == Activity.RESULT_CANCELED) {
Log.v("CANCELED", "The user has canceled the activity");
}
}
}
我的 logcat 说,它不仅不起作用
06-13 20:53:36.320: V/RESULT CODE(6140): 0
06-13 20:53:36.320: V/REQUEST CODE(6140): -1
为什么会这样?有人遇到过这个吗?