简体   繁体   English

片段的onActivityResult方法中的意图值null

[英]intent value null in onActivityResult method of the fragment

I started working on capturing cam image using file provider. 我开始使用文件提供程序捕获凸轮图像。 My fragment calls cam capture intent as per the android docs related to take image simply . 我的片段根据与简单拍摄图像有关的android文档调用cam capture intent。

The issue is in onActivityResult method intent returns null. 问题在于onActivityResult方法意图返回null。 Here is how I set everything. 这是我设置所有东西的方式。 I am not able to figure out what I am doing wrong. 我无法弄清楚我在做什么错。

AndroidManifest.xml AndroidManifest.xml

 <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_path" />
  </provider>

file_path.xml file_path.xml

<paths>
    <external-files-path name="my_images" />
</paths>

Implementation on camcapture intent in Fragment.class Fragment.class中关于捕获意图的实现

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File photoFile = null;

    try {
        photoFile = Utils.createImageFile(getContext());
        String authorities = context.getPackageName()+".fileprovider";
        uriForFile = FileProvider.getUriForFile(context, authorities, photoFile);
    } catch (IOException e) {
        e.printStackTrace();
    }

    Log.d(TAG, "handleCaptureImage: "+uriForFile); // shows this result in logs content://appid.fileprovider/my_images/Pictures/JPEG_20170918_181145_719404055.jpg
    intent.putExtra(MediaStore.EXTRA_OUTPUT, uriForFile);
    startActivityForResult(intent, OPEN_CAMERA);

onActivityResult in fragment 片段中的onActivityResult

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

    if (resultCode == RESULT_OK) {

        if (requestCode == PICK_IMAGE) {
            try {
                //works fine bitmap generated correctly.
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), data.getData());
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else if (requestCode == OPEN_CAMERA) {
            Log.d(TAG, "onActivityResult: "+data); //data always null
        }
    }
}

I tried calling onActivityResult() in hosting activity as but same result. 我尝试在托管活动中调用onActivityResult(),但结果相同。 Need pointer in right direction.. Thank you in advance. 需要正确的方向指针。

This will be helpful data in onActivityResult is null 这将对onActivityResult中的数据有帮助

And If data.getData() returns null then try: 如果data.getData()返回null,请尝试:

    Bundle extras = data.getExtras();
    Bitmap bitmap = (Bitmap) extras.get("data");

Hope that helps :) 希望有帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM