Android onActivityResult无法正常工作

时间:2014-10-13 05:09:36

标签: android android-activity

我不确定为什么我的代码在完成已启动的活动时不会调用onActivityResult

这是调用类:

public class RecognitionActivity extends Activity {

static final int NEW_PICTURE_REQUEST = 1;
static final int RECOGNISE_TEXT_REQUEST = 2;

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

    startCamera();
}

private void startCamera() {
    Intent cameraIntent = new Intent(this, CameraActivity.class);
    startActivityForResult(cameraIntent, NEW_PICTURE_REQUEST);
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Toast.makeText(this, requestCode + ":" + resultCode, Toast.LENGTH_SHORT).show();
}
}

以下是 CameraActivity 类,或者至少是相关部分:

public class CameraActivity extends Activity {

private Camera camera;
private CameraPreview cameraPreview;

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

    camera = Camera.open();
    cameraPreview = new CameraPreview(this, camera);

    FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
    preview.addView(cameraPreview);

    Button captureButton = (Button) findViewById(R.id.button_capture);
    captureButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent returnIntent = new Intent();
            setResult(RESULT_OK, returnIntent);
            finish();
        }
    });
}
}

奇怪的是,当cameraActivity活动结束时,调用活动(RecognitionActivity)也没有看到ToastonActivityResult突然出现?< / p>

编辑:这是RecognitionActivity Manifest:

    <activity
        android:name=".RecognitionActivity"
        android:label="@string/title_activity_recognition"
        android:screenOrientation="landscape">
    </activity>

EDIT2:忽略这一点,我想通了,我发现了。从我的启动器打开cameraActivity而不是recognActivity。旧的测试代码。该死。

3 个答案:

答案 0 :(得分:1)

 On button click we can start the camera intent  startActivityForResult(cameraIntent,ACTION_REQUEST_CAMERA);
    and fetch the image path using content resolver.

        @Override
            protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                // TODO Auto-generated method stub
                super.onActivityResult(requestCode, resultCode, data);
                 if (resultCode == RESULT_OK &&  data!=null) {
                if (requestCode == ACTION_REQUEST_CAMERA) {
                    Uri selectedImage = data.getData();
                    String[] filePathColumn = { MediaStore.Images.Media.DATA };

                    Cursor cursor = getContentResolver().query(selectedImage,
                            filePathColumn, null, null, null);
                    cursor.moveToFirst();

                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    picturepath = cursor.getString(columnIndex);
                    cursor.close();


                }

                else    if (requestCode == ACTION_REQUEST_GALLERY) 
                {
                     Uri selectedImage = data.getData();
                        String[] filePathColumn = { MediaStore.Images.Media.DATA };

                        Cursor cursor = getContentResolver().query(selectedImage,
                                filePathColumn, null, null, null);
                        cursor.moveToFirst();

                        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                         picturepath = cursor.getString(columnIndex);
                        cursor.close();

            }

答案 1 :(得分:1)

我认为你的意图需要这样的东西,

    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

    startActivityForResult(cameraIntent, NEW_PICTURE_REQUEST);

这可能会帮助你.. http://developer.android.com/training/camera/photobasics.html

答案 2 :(得分:0)

尝试在CameraActivity中评论以下区域......

private Camera camera;
private CameraPreview cameraPreview;

camera = Camera.open();
cameraPreview = new CameraPreview(this, camera);

FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(cameraPreview);

然后运行项目...我认为那会工作......我的工作也是...... 所以,问题在于你的相机逻辑......你必须改进你的代码,参考@shristi