我正在启动相机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
为什么会这样?有没有人遇到过这个?
答案 0 :(得分:3)
这是正确的onActivityResult
以及问题的原因:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {}
VS你的:
@Override
public void onActivityResult(int resultCode, int requestCode, Intent data) {}