在片段中裁剪图像

时间:2016-05-16 20:48:51

标签: java android image crop

我正在使用此库来裁剪图片https://android-arsenal.com/details/1/3487 尝试从片段中裁剪图像而不是从常规活动中裁剪时出现问题但是它不起作用,即使我做了确切的事情, 这是我的onActivityResult

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == getActivity().RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {



            CropImage.activity(data.getData())
                    .setCropShape(CropImageView.CropShape.OVAL)
                    .setGuidelines(CropImageView.Guidelines.OFF)
                    .setAutoZoomEnabled(false)
                    .start(getContext(), FeedFragment.this);

        }

        if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
            CropImage.ActivityResult result = CropImage.getActivityResult(data);
            try {
                Uri resultUri = result.getUri();
                InputStream is = getActivity().getContentResolver().openInputStream(resultUri);
                Bitmap photoBitmap = BitmapFactory.decodeStream(is);
                imgButton.setImageBitmap(photoBitmap);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
}

问题出在这一行:

  .start(getContext(), FeedFragment.this);

错误:

Failure delivering result ResultInfo{who=null, request=65537, result=-1, data=Intent { dat=content://media/external/images/media/5560 flg=0x1 (has extras) }} to activity {apps.berant.knowu/apps.berant.knowu.MainActivity}: android.content.ActivityNotFoundException: Unable to find explicit activity class {apps.berant.knowu/com.theartofdev.edmodo.cropper.CropImageActivity}; have you declared this activity in your AndroidManifest.xml?

感谢您的帮助:)

3 个答案:

答案 0 :(得分:5)

添加行AndroidManifest.xml

<activity android:name="com.theartofdev.edmodo.cropper.CropImageActivity"/>

答案 1 :(得分:1)

正如在文档中提到的,我只是简单地将其添加到清单中即可:

<activity
        android:name="com.yalantis.ucrop.UCropActivity"
        android:screenOrientation="portrait"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>

答案 2 :(得分:0)

<android.support.v7.widget.AppCompatImageView
    android:id="@+id/iv_house"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:scaleType="fitXY"/> 

在片段中

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.iv_house:
    if (CropImage.isExplicitCameraPermissionRequired(getActivity())) {
        requestPermissions(new String[]{Manifest.permission.CAMERA}, CropImage.CAMERA_CAPTURE_PERMISSIONS_REQUEST_CODE);
    } else {
        CropImage.startPickImageActivity(getActivity());
    }
    break;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == CropImage.PICK_IMAGE_CHOOSER_REQUEST_CODE) {
        Uri imageUri = CropImage.getPickImageResultUri(getActivity(), data);
        if (CropImage.isReadExternalStoragePermissionsRequired(getActivity(), imageUri)) {
            mCropImageUri = imageUri;
            requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, CropImage.PICK_IMAGE_PERMISSIONS_REQUEST_CODE);    
        } else {
            Intent intent = CropImage.activity(imageUri)
                .setGuidelines(CropImageView.Guidelines.ON)
                .setFixAspectRatio(true)
                .setAspectRatio(2,1)
                .getIntent(getActivity());
            getActivity().startActivityForResult(intent, CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE);
        }
    }

    if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
        CropImage.ActivityResult result = CropImage.getActivityResult(data);
        if (resultCode == RESULT_OK) {
            Uri resultUri = result.getUri();
            selectedFilePath = resultUri.getPath();
            imageView.setImageURI(resultUri);
        } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
            Exception error = result.getError();
            Toast.makeText(getActivity(), error.getMessage(), Toast.LENGTH_LONG).show();
        }
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    //super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == CropImage.CAMERA_CAPTURE_PERMISSIONS_REQUEST_CODE) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            CropImage.startPickImageActivity(getActivity());
        } else {
            Toast.makeText(getActivity(), "Cancelling, required permissions are not granted", Toast.LENGTH_LONG).show();
        }
    }

    if (requestCode == CropImage.PICK_IMAGE_PERMISSIONS_REQUEST_CODE) {
        if (mCropImageUri != null && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            Intent intent = CropImage.activity(mCropImageUri)
                .setGuidelines(CropImageView.Guidelines.ON)
                .setFixAspectRatio(true)
                .setAspectRatio(2,1)
                .getIntent(getActivity());
            getActivity().startActivityForResult(intent, CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE);
        } else {
            Toast.makeText(getActivity(), "Cancelling, required permissions are not granted", Toast.LENGTH_LONG).show();
        }
    }
}

活动中

private Uri mCropImageUri;  

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    //super.onActivityResult(requestCode, resultCode, data);
    Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.container);
    fragment.onActivityResult(requestCode, resultCode, data);
}

在AndroidManifest.xml

<activity
    android:name="com.theartofdev.edmodo.cropper.CropImageActivity"
    android:theme="@style/Base.Theme.AppCompat" />