onActivityForResult不适用于主选项卡活动

时间:2018-10-10 19:37:55

标签: android

我有一个问题,我在主选项卡活动(而不是片段)中从onActivityResult始终获取空值。下面的代码如下

我有一个fab按钮,调用方法openCamera()

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (ContextCompat.checkSelfPermission(TabActivity.this, Manifest.permission.CAMERA)
                    != PackageManager.PERMISSION_GRANTED){
                ActivityCompat.requestPermissions(TabActivity.this,
                        new String[]{Manifest.permission.CAMERA},
                        0);
            }else {
                openCamera();
            }
        }
    });

然后该方法尝试打开相机,然后将其传递给onActivityResult

private void openCamera(){
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    TabActivity.this.startActivityForResult(intent, CAMERA_REQUEST_CODE);
}

但是它返回一个空值

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK){

        Uri uri = data.getData();//<--This returns a null
    }
}

0 个答案:

没有答案