data.getData()在从相机和图库中选择/选择图片时在OnActvityResult中返回null

时间:2017-04-28 16:19:44

标签: android android-intent android-camera android-camera-intent android-intent-chooser

我正在开发一个Android应用程序,它有一个配置文件片段,用户可以通过拍照或从库中选择上传个人资料图片。现在,一切都通过Activity.RESULT_OK正常工作。但问题是onActivityResult data.getData()没有返回任何内容。我对这个问题的研究无济于事。

这是我的editprofile片段中包含的两个方法。

public void onActivityResult(Edit_profile_fragment myEdit_profile_fragment, int requestCode, int resultCode, Intent data) {
    String imagePath = null;
    if (resultCode == Activity.RESULT_OK) {
        if (PermissionHandler.checkPermission(myEdit_profile_fragment.getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE)) {
            AppHelper.LogCat("Read contact data permission already granted.");

            switch (requestCode) {
                case AppConst.SELECT_PROFILE_PICTURE:
                        imagePath = FilesManager.getPath(myEdit_profile_fragment.getActivity(), data.getData());
                    break;
                case AppConst.SELECT_PROFILE_CAMERA:
                    if (data.getData() != null) {
                        imagePath = FilesManager.getPath(myEdit_profile_fragment.getActivity(), data.getData());

                    } else {
                        try {
                            String[] projection = new String[]{MediaStore.Images.ImageColumns._ID, MediaStore.Images.ImageColumns.DATA, MediaStore
                                    .Images.ImageColumns.BUCKET_DISPLAY_NAME, MediaStore.Images.ImageColumns.DATE_TAKEN, MediaStore.Images
                                    .ImageColumns.MIME_TYPE};
                            final Cursor cursor = myEdit_profile_fragment.getActivity().getContentResolver()
                                    .query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, MediaStore.Images.ImageColumns
                                            .DATE_TAKEN + " DESC");

                            if (cursor != null && cursor.moveToFirst()) {
                                String imageLocation = cursor.getString(1);
                                cursor.close();
                                File imageFile = new File(imageLocation);
                                if (imageFile.exists()) {
                                    imagePath = imageFile.getPath();
                                }
                            }
                        } catch (Exception e) {
                            AppHelper.LogCat("error" + e);
                        }
                    }
                    break;
            }


            if (imagePath != null) {
                EventBus.getDefault().post(new Pusher(AppConst.EVENT_BUS_IMAGE_PROFILE_PATH, imagePath));
            } else {
                AppHelper.LogCat("imagePath is null");
            }
        } else {
            AppHelper.LogCat("Please request Read contact data permission.");
            PermissionHandler.requestPermission(myEdit_profile_fragment.getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE);
        }
    }

}

这是我的EditProfilePresenter.onActivityResult方法。

UnboundLocalError: local variable 'response' referenced before assignment on line 7

请问我出错了什么?

1 个答案:

答案 0 :(得分:0)

你可以试试这个

if (requestCode == REQUEST_FROM_CAMERA && resultCode == RESULT_OK)
{
Bundle extras2 = data.getExtras();
 if (extras2 != null) {
                    // do your stuff here
                     } 
                   else {
                  // handle this case as well if data.getExtras() is null
                    Uri selectedImage = data.getData();
                   }
}

希望它可以帮到你