从片段中捕获图像

Capture Image from Fragment

我需要从相机捕捉图像,并且我需要从 Fragment 中进行。我的问题是永远不会调用 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);
}

我在这个论坛上查找了几个主题,但其中 none 对我有用。

您还必须在 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);
}

您可以像下面这样在片段中调用它首先将代码放入您创建 fragmentactivityonActivityResult() 方法中,如下所示:

@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() 方法,它就会被调用

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

public class CameraPic extends Fragment {

    private static final int CAPTURE_PICCODE = 989;
    Button button;
    ImageView imageView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        final View rootView = inflater.inflate(R.layout.cameralayout,
                container, false);

        button = (Button) rootView.findViewById(R.id.button1);
        imageView = (ImageView) rootView.findViewById(R.id.imageview1);

        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent,
                        CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

            }
        });

        return rootView;

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
            if (resultCode == Activity.RESULT_OK) {

                Bitmap bmp = (Bitmap) data.getExtras().get("data");
                ByteArrayOutputStream stream = new ByteArrayOutputStream();

                bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
                byte[] byteArray = stream.toByteArray();



                Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0,
                        byteArray.length);

                imageView.setImageBitmap(bitmap);

            }
        }        
    }    
}

检查您是否没有覆盖 Activity 中的 onActivityResult。如果是这样,您应该从此方法调用 super.onActivityResult。按照这个问题来了解详情:onActivityResult is not being called in Fragment