Android crop action getData null with some device

708 views Asked by At

I have a problem when cropping image with activity result. I have set permission on device >6. But when show crop and crop is successful, the result data is null. (imageReturnedIntent.getData() us null and intent data is also null). It is code crop image.

public static Intent cropIntent(Uri inUri, int outputX, int outputY,
                                    boolean isScale) {
    Intent cropIntent = new Intent("com.android.camera.action.CROP");
    cropIntent.setDataAndType(inUri, "image/*");
    cropIntent.putExtra("crop", "true");
    cropIntent.putExtra("aspectX", outputX > 0 ? outputX : 100);
    cropIntent.putExtra("aspectY", outputY > 0 ? outputY : 100);
    cropIntent.putExtra("outputX", outputX > 0 ? outputX : 100);
    cropIntent.putExtra("outputY", outputY > 0 ? outputY : 100);
    cropIntent.putExtra("scale", isScale);
    File file = new File(Environment.getExternalStorageDirectory() + File.separator + "img.jpg");
    Uri mCropImagedUri = Uri.fromFile(file);
    cropIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mCropImagedUri);
    cropIntent.putExtra("return-data", true);
    return cropIntent;
}

It is code activitysult

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
    if (callbackManager != null) {
        callbackManager.onActivityResult(requestCode, resultCode, imageReturnedIntent);

    }

    switch (requestCode) {
        case SELECT_PHOTO:
            if (resultCode == RESULT_OK) {

                Uri uriImage = imageReturnedIntent.getData();
                System.out.println(">>>> uri "+uriImage);
                //   CommonUtils.cropImage(uriImage,HomeActivity.this,SELECT_CROP_IMAGE);
                startActivityForResult(CommonUtils.cropIntent(uriImage,
                        200, 200, true), SELECT_CROP_IMAGE);
            }
            break;
        case SELECT_CROP_IMAGE:
            if (resultCode == RESULT_OK) {
                //   Uri uri = imageReturnedIntent.getData();

                System.out.println(">>>>> "+imageReturnedIntent.getData());
                Bundle extras = imageReturnedIntent.getExtras();
                if (extras == null) {
                    System.out.println(">>>>> check intent");
                    return;
                }
                // Bitmap bitMapScale = extras.getParcelable("data");
                Uri uri = extras.getParcelable(MediaStore.EXTRA_OUTPUT);
                try {
                    Bitmap bitMapScale = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
                    System.out.println(">>>>>> check " + bitMapScale);
                    //       yourSelectedImage.recycle();
                    // base64
                    String myBase64Image = CommonUtils.encodeToBase64(bitMapScale, Bitmap.CompressFormat.PNG, 100);
                    //   bitMapScale.recycle();
                    assert uri != null;
                    LocalStorage.getShareInstance().saveUriAvatar(uri.toString());
                    LocalStorage.getShareInstance().saveBase64Image(myBase64Image);
                    UserModel userModel = LocalStorage.getShareInstance().getUserLogin();
                    if (userModel == null) {
                        // if user have not login.
                        if (uiLeftMenu != null) {
                            uiLeftMenu.fillImage(uri);
                        }
                    } else {
                        System.out.println(">>>>>> check data");
                        if (CommonUtils.isStringDataValid(myBase64Image)) {

                            upLoadImageToServerWithStringBase64(myBase64Image);
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }


            }
            break;
    }
}

What is the problem?

1

There are 1 answers

0
Abu Yousuf On

I also faced same problem on Huwai device. Some device don't sent destination Uri via intent.getData(). But after some debugging i found out that the uri is set on mAction String variable and it can be accessed via intent.getAction() method. Then you can generate Uri with 'Uri.parse(outPutFilePath)' this method. You can first check intent.getData() is null or not. If null then use intent.getAction() method to get Uri as string and generate output Uri. I solved my problem this way. Hope this will help.