使用CyanogenMod的Android挑选和裁剪图像oneplusone

时间:2017-04-06 13:53:09

标签: android crop android-gallery cyanogenmod oneplusone

在我的Android应用程序中,用户可以选择图像并裁剪它。 我的代码工作正常,直到我听说有人使用带有CynogenMod的oneplusone智能手机的所有其他智能手机出现问题似乎工作正常。

问题是如果他点击启动我的onClick的按钮,应用程序就会停止工作。

我正在使用com.theartofdev.edmodo.cropper裁剪我的图片。

public void onClick(View v){
    Permissions.verifyStoragePermissions(this);
    Intent galleryIntent = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    try {
        if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
                && null != data) {
            Uri uri = data.getData();
            this.performCrop(uri);

        } else if (requestCode == RESULT_CROP_IMG) {
            if (data != null) {
                ImageView imgView = (ImageView) findViewById(R.id.imageView_my_profile);
                imgView.setImageURI(data.getData());
            }


        } else if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
            CropImage.ActivityResult result = CropImage.getActivityResult(data);
            ImageView imgView = (ImageView) findViewById(R.id.imageView_my_profile);
            if (resultCode == RESULT_OK) {
                Uri resultUri = result.getUri();
                imgView.setImageURI(resultUri);
            } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
                Exception error = result.getError();
            }
        }
    } catch (Exception e) {
        Toast.makeText(this, getString(R.string.global_default_error), Toast.LENGTH_LONG)
                .show();
    }

}

private void performCrop(Uri picUri) {
    ImageView imgView = (ImageView) findViewById(R.id.imageView_my_profile);
    CropImage.activity(picUri).setAspectRatio(imgView.getWidth(), imgView.getHeight())
            .setGuidelines(CropImageView.Guidelines.OFF).setCropShape(CropImageView.CropShape.OVAL)
            .start(this);
}

这是我用来验证权限的内容:

private static final int REQUEST_EXTERNAL_STORAGE = 3;
private static String[] PERMISSIONS_STORAGE = {
        Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.WRITE_EXTERNAL_STORAGE
};

public static void verifyStoragePermissions(Activity activity) {
    int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

    if (permission != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(
                activity,
                PERMISSIONS_STORAGE,
                REQUEST_EXTERNAL_STORAGE
        );
    }
}

0 个答案:

没有答案