从片段中捕获图像

时间:2016-05-04 09:47:40

标签: android android-fragments android-camera android-camera-intent

我需要从相机中捕捉图像,我需要从片段内部进行拍摄。我的问题是永远不会调用onActivityResult。以下是我的代码:

private void dispatchTakePictureIntent(){
  Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
  }



@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
  if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {
     Bundle extras = data.getExtras();
     Bitmap imageBitmap = (Bitmap) extras.get("data");
     im.setImageBitmap(imageBitmap);
  }
  Log.e(TAG,"hello");
  super.onActivityResult(requestCode, resultCode, data);
}

我在这个论坛上查了几个主题,但没有一个在我这里工作。

4 个答案:

答案 0 :(得分:1)

您还必须在onActivityResult中传递super.onActivityResult(requestCode, resultCode, data);

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
  if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {
     Bundle extras = data.getExtras();
     Bitmap imageBitmap = (Bitmap) extras.get("data");
     im.setImageBitmap(imageBitmap);
  }
  Log.e(TAG,"hello");
  super.onActivityResult(requestCode, resultCode, data);
}

答案 1 :(得分:1)

我试过这种方式,这对我有用

<ListBox x:Name="MyList"></ListBox>
<TextBox Text="{Binding ElementName=MyList, Path=SelectedItem.Property}"/>

答案 2 :(得分:0)

您可以执行以下操作以在片段中调用它首先将代码放入onActivityResult() activity方法中,您已创建fragment,如下所示:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Fragment fragment = getSupportFragmentManager().findFragmentByTag("Your Fragment Tag");
        fragment.onActivityResult(requestCode, resultCode, data);
    }

现在在你的片段中放入onActivityResult()方法,它将被称为

答案 3 :(得分:0)

检查您是否在活动中覆盖onActivityResult。如果是这样,您应该从此方法中调用super.onActivityResult。请按照此问题查找详细信息:onActivityResult is not being called in Fragment