Search code examples
androidandroid-intentandroid-permissionsandroid-image-capture

Intent object on onActivityreslut goes null on Image capture android


I am trying to set a image as profile picture on image view after capture using camera. image capturing is working fine and image is stored in device memory. But on onActivityResult code intent object goes null.

Here is the code for call camera action

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

which is calling from a dialoge and in 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);
        }
    }

In fragment

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

in activity and fragment data came as null.


Solution

  • But on onActivityResult code intent object goes null.

    It is supposed to be null. You know where the image should be located: it is where you specified in EXTRA_OUTPUT. Look there for the image.

    Also note that Uri.fromFile() will not work for you on Android 7.0+, once your targetSdkVersion climbs to 24 or higher. Please switch to using FileProvider and its getUriForFile() method. This sample app from this book illustrates how to do this.