gpt4 book ai didi

android - 在 Android 上使用相机时,onActivityResult 无法与 RESULT_OK、RESULT_CANCEL 等一起使用

转载 作者:行者123 更新时间:2023-11-30 04:37:37 25 4
gpt4 key购买 nike

我的课上有这样的东西:

  public class Main extends Activity {

private static final int CAMERA_PICK = 1;
private static final int GALLERY_PICK = 2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button photo = (Button) findViewById(R.id.button);

photo.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
createDialog();
}
});

private void createdialog(Activity activity) {
final CharSequence[] items = { "Take shot", "Take from gallery" };

AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle("Get image");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
if (item == 0) {
takePhoto();
}
if (item == 1) {
choosePhoto();
}
}
});
AlertDialog alert = builder.create();
alert.show();
}

protected void choosePhoto() {
// not necessary;
}

protected void takePhoto() {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

mUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "myPic"
+ String.valueOf(System.currentTimeMillis()) + ".jpg"));
cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mUri);

try {
cameraIntent.putExtra("return-data", true);
startActivityForResult(cameraIntent, CAMERA_PICK);

//Doing something with the picture here;
}
} catch (Exception e) {
e.printStackTrace();
}
}

// TODO
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

switch (resultCode) {
case CAMERA_PICK:
break;
case RESULT_OK:
Toast.makeText(Main.this, "Photo selected", Toast.LENGTH_SHORT).show();
break;
}
}
}

如您所见,我正在尝试使用手机的摄像头拍照并稍后在 ImageView 中使用它。问题是我无法触发方法 onActivityResult(...)!当我执行 startActivityForResult(cameraIntent, CAMERA_PICK);我没有办法操纵 RESULT_OK、RESULT_CANCEL 甚至我定义为 CAMERA_PICK 的那个。 onActivityResult(...) 应该完美工作,因为我不明白我做错了什么!

如有任何帮助,我们将不胜感激。

I've already found the problem. I was using an activity group and I didn't realized that the onActivityResult() triggered was the first one of all the activities...

最佳答案

在某些真实设备上从图库中挑选照片时,我遇到了同样的问题。经过大量搜索,我找到了 this .表示可能会触发 Dialog 的 onActivityResult 而不是 Activity 来获取返回值。此问题不会在所有设备上发生。

关于android - 在 Android 上使用相机时,onActivityResult 无法与 RESULT_OK、RESULT_CANCEL 等一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6576399/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com