从片段中捕获图像

Posted

技术标签:

【中文标题】从片段中捕获图像【英文标题】:Capture Image from Fragment 【发布时间】:2016-08-29 16:18:27 【问题描述】:

我需要从相机中捕获图像,并且需要从 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);

我在这个论坛上查找了几个主题,但没有一个对我有用。

【问题讨论】:

onActivityResult For Fragment的可能重复 【参考方案1】:

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

【讨论】:

【参考方案2】:

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

【讨论】:

【参考方案3】:

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

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

            
                
        

【讨论】:

【参考方案4】:

您可以执行以下操作以在片段中调用它首先将代码放入您创建 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() 方法,它就会被调用

【讨论】:

以上是关于从片段中捕获图像的主要内容,如果未能解决你的问题,请参考以下文章

从片段捕获图像时从不调用onActivityResult [重复]

Android:在捕获或从图库中挑选后裁剪图像

如何在不将图像保存在本地的情况下将捕获的图像(Surface View)传递给另一个片段?

从片段中的相机意图返回后屏幕变为白色

从图库中获取图像以在片段中的图像视图中设置? [复制]

使用片段从图库中选择图像