在Android上使用相机时,onActivityResult无法使用RESULT_OK,RESULT_CANCEL等

时间:2011-07-04 22:01:04

标签: android camera switch-statement photo

我的班级里面有这样的东西:

  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;
        }
       }
    }

正如您所看到的,我正在尝试使用手机的相机拍照并稍后在图像视图中使用它。事情是我无法触发方法 onActivityResult(...)!当我做startActivityForResult(cameraIntent,CAMERA_PICK);我无法操纵RESULT_OK,RESULT_CANCEL甚至是我定义为CAMERA_PICK的方法。 onActivityResult(...)应该是完美的,我不明白我做错了什么!

任何帮助都会得到帮助,谢谢。

  

我已经发现了这个问题。我正在使用一个活动小组和我   没有意识到onActivityResult()触发了第一个   其中一项活动......

1 个答案:

答案 0 :(得分:1)

在某些真实设备上从图库中选择照片时,我遇到了同样的问题。经过大量搜索后,我找到了this。指示对话框的onActivityResult可能是tirggered而不是获取返回值的活动。所有设备上都不会出现此问题。