在片段中裁剪图像

Posted

技术标签:

【中文标题】在片段中裁剪图像【英文标题】:Crop image in a fragment 【发布时间】:2016-05-16 20:48:50 【问题描述】:

我正在使用这个库来裁剪图像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 ResultInfowho=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?

感谢您的帮助:)

【问题讨论】:

你至少读过一个例外吗? 您是否在 AndroidManifest.xml 中声明了此活动 结果数据被转储到父活动 onActivityResult() 中。阅读以下内容:***.com/questions/6147884/… 【参考方案1】:

添加行AndroidManifest.xml

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

【讨论】:

【参考方案2】:
<android.support.v7.widget.AppCompatImageView
    android:id="@+id/iv_house"
    android:layout_
    android:layout_
    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" />

【讨论】:

【参考方案3】:

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

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

【讨论】:

以上是关于在片段中裁剪图像的主要内容,如果未能解决你的问题,请参考以下文章

Android图像视图顶部裁剪

Android:裁剪图像并在 ImageView 上显示

如何从 RCNN 中裁剪分割的对象?

我想裁剪图像并输入在图像中查看。如何使用这种类型的裁剪从相机裁剪图像?

ListView 背景图片裁剪

在 WooCommerce 中禁用原始图像裁剪