片段的onActivityResult方法中的intent值为null

时间:2017-09-18 22:25:13

标签: java android android-fragments android-intent android-fileprovider

我开始使用文件提供程序捕获凸轮图像。我的片段根据与take image simply相关的android文档调用cam捕获意图。

问题出在onActivityResult方法意图返回null。这是我如何设置一切。我无法弄清楚我做错了什么。

的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

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

Fragment.class

中实现camcapture意图
    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

@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
        }
    }
}

我尝试在托管活动中调用onActivityResult(),但结果相同。需要指向正确的方向..提前谢谢你。

1 个答案:

答案 0 :(得分:0)

这会有所帮助 data in onActivityResult is null

如果data.getData()返回null,则尝试:

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

希望有所帮助:)