Image Cropper无法从OnActivityResult启动新活动

时间:2017-11-28 13:15:49

标签: android image

我正在尝试从包含我本地图库图像的GridView的GalleryActivity中选择一个图像并启动CropActivity,我正在使用一个名为Android Image Cropper的外部库。裁剪后,我想启动一个新的ConfirmPhotoActivity来确认图像并开始上传到服务器等。

现在,在我裁剪图像并单击裁剪按钮后,它总是将我带回GalleryActivity,并且logcat不会抛出任何错误消息......

请参阅以下相关代码:

GalleryActivity:

    //set onClickListener to the grid image view.
    mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Log.d(TAG, "onItemClick: selected an image " + imageURLs.get(position));

            Uri imageUri = Uri.parse(mAppend + imageURLs.get(position));
            startCrop(imageUri);
            Log.d(TAG, "onItemClick: starting crop image activity");

        }
    });

}

private void startCrop(Uri imageUri){
    CropImage.activity(imageUri)
            .setAspectRatio(1,1)
            .start(this);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
        CropImage.ActivityResult result = CropImage.getActivityResult(data);
        if (resultCode == RESULT_OK && data !=null) {
            resultUri = result.getUri();

            Log.d(TAG, "onActivityResult: imageUrl is" + resultUri.toString());
            String imageUrl = resultUri.toString();
            startActivity(new Intent(this, ConfirmPhotoActivity.class).putExtra("croppedImageUrl", imageUrl));

        } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
            Exception error = result.getError();
        }


    }

}

ConfirmPhotoActivity

public class ConfirmPhotoActivity extends AppCompatActivity {

SquareImageView squareImageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_confirm_photo);

    squareImageView = findViewById(R.id.confirm_photo_imageView);

    Intent intent = getIntent();
    String imageurl = intent.getStringExtra("croppedImageUrl");
    ImageLoader imageLoader = ImageLoader.getInstance();
    imageLoader.displayImage(imageurl, squareImageView, new ImageLoadingListener() {
        @Override
        public void onLoadingStarted(String imageUri, View view) {

        }

        @Override
        public void onLoadingFailed(String imageUri, View view, FailReason failReason) {

        }

        @Override
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {

        }

        @Override
        public void onLoadingCancelled(String imageUri, View view) {

        }
    });





}

非常感谢!

1 个答案:

答案 0 :(得分:0)

试试这个

 @SuppressLint("NewApi")
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {

        // handle result of pick image chooser
        if (requestCode == CropImage.PICK_IMAGE_CHOOSER_REQUEST_CODE && resultCode == Activity.RESULT_OK)
        {
            Uri imageUri = CropImage.getPickImageResultUri(this, data);

            // For API >= 23 we need to check specifically that we have permissions to read external storage.
            if (CropImage.isReadExternalStoragePermissionsRequired(this, imageUri))
            {
                // request permissions and handle the result in onRequestPermissionsResult()
                mCropImageUri = imageUri;
                requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 0);
            }
            else
            {
                // no permissions required or already grunted, can start crop image activity
                startCropImageActivity(imageUri);
            }
        }

        // handle result of CropImageActivity
        if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE)
        {
            CropImage.ActivityResult result = CropImage.getActivityResult(data);
            if (resultCode == RESULT_OK)
            {
                profileIV.setImageURI(result.getUri());

                /*Here you can start new activity by passing imageUri with intent*/
                profileImageFilepath = result.getUri().getPath();
                Log.d("cropImageUri", result.getUri().getPath());
            }
            else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE)
            {
                Toast.makeText(this, "Failed to select photo", Toast.LENGTH_LONG).show();
            }
        }
    }