2

使用活动结果裁剪图像时出现问题。我已在设备 >6 上设置权限。但是当显示裁剪和裁剪成功时,结果数据为空。(imageReturnedIntent.getData()我们为空,意图数据也为空)。这是代码裁剪图像。

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

这是代码活动结果

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

问题是什么?

4

1 回答 1

0

我在Huwai设备上也遇到了同样的问题。某些设备不会通过intent.getData(). 但是经过一些调试后,我发现 uri 是在mActionString 变量上设置的,并且可以通过intent.getAction()方法访问它。然后你可以用'Uri.parse(outPutFilePath)'这个方法生成Uri。您可以先检查是否intent.getData()为空。如果为 null,则使用intent.getAction()方法将 Uri 获取为字符串并生成输出 Uri。我以这种方式解决了我的问题。希望这会有所帮助。

于 2017-11-19T10:53:13.820 回答