onActivityreslut 上的 Intent 对象在图像捕获时变为空 android

Intent object on onActivityreslut goes null on Image capture android

我正在尝试在使用相机拍摄后在图像视图中将图像设置为个人资料图片。图像捕捉工作正常,图像存储在设备内存中。但是在Activity结果代码意图对象变为空。

这里是调用相机动作的代码

 Intent camera = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                    Uri file = Uri.fromFile(Utilities.getOutputMediaFile());
                    camera.putExtra(MediaStore.EXTRA_OUTPUT, file);
                    activity.startActivityForResult(camera, Constants.IMAGE_CAPTURE_CAMERA);

这是从对话中调用的 Activity

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constants.LOGIN_REQUEST) {
            if (AppSettings.getInstance(HomeScreen.this).getBooleanValue(AppKeys.LOGIN_STATUS)) {changeFragment(new Fragment_Account());
            }
        } else {
            Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.frameContainer);
            fragment.onActivityResult(requestCode, resultCode, data);
        }
    }

片段中

 @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null) {
            if (requestCode == Constants.IMAGE_CAPTURE_CAMERA && resultCode == Activity.RESULT_OK) {
                ProfilePicUri = (Uri) data.getExtras().get(MediaStore.EXTRA_OUTPUT);
                Log.d("ImageUri", "onActivityResult: " + ProfilePicUri);
                performCrop();
            } else if (requestCode == Constants.IMAGE_CAPTURE_GALLERY) {
                if (data.getData() != null) {
                    ProfilePicUri = data.getData();
                    performCrop();
                }
            } else if (requestCode == Constants.IMAGE_CROP) {
                Bundle extras = data.getExtras();
                if (extras != null) {
                    Bitmap newProfilePic = extras.getParcelable("data");
                    if (newProfilePic != null) {
                        customer.setProfilePic(Utilities.encodeTobase64(newProfilePic));
                        profilePic.setImageBitmap(newProfilePic);
                        Bitmap image = ((BitmapDrawable) profilePic.getDrawable()).getBitmap();
                        String imageUrls = Utilities.encodeTobase64(image);
                        String imageString = image.toString();
                        Log.d("imageUrl : ", imageUrls);
                    }
                }
            }
        }
    }

在 activity 中,片段 data 为空。

Remove the line where you set the path to save your file unless you need to set your custom path to save your file.

一旦

设置了 URI
Intent.putExtra(MediaStore.EXTRA_OUTPUT, file);

最终的 intent 将为 null,它的设计目的就是那样执行。

onActivityResult,

intent.getData() 

将为您提供捕获图像的 URI。

But on onActivityResult code intent object goes null.

应该是null。您知道图像的位置:它是您在 EXTRA_OUTPUT 中指定的位置。在那里寻找图像。

另请注意,一旦您的 targetSdkVersion 攀升至 24 或更高,Uri.fromFile() 将不适用于 Android 7.0+。请切换到使用 FileProvider 及其 getUriForFile() 方法。 This sample app from this book 说明了如何执行此操作。