简体   繁体   English

裁剪意图在 Android Studio 中不起作用

[英]Crop Intent Not Working in Android Studio

I have a problem regarding cropping image in android studio, whenever I run this cropping code on my device or any other One-plus device, it runs efficiently.我在 android studio 中遇到裁剪图像的问题,每当我在我的设备或任何其他一加设备上运行此裁剪代码时,它都能高效运行。 But other devices like Redmi, Samsung, Motorola, crash after reaching this cropping part.但其他设备,如 Redmi、三星、摩托罗拉,在到达此裁剪部分后会崩溃。 if I comment out the function call to the cropping function, it runs smoothly on all devices but at cost of non-availability of cropping如果我注释掉 function 对裁剪 function 的调用,它会在所有设备上顺利运行,但代价是无法进行裁剪

public void ImageCropFunction(Uri uri) {

        // Image Crop Code
        try {
            Intent CropIntent = new Intent("com.android.camera.action.CROP");
            Toast.makeText(getContext(),"plz, Crop the Required part",Toast.LENGTH_LONG).show();
            CropIntent.setDataAndType(uri, "image/*");
            CropIntent.putExtra("crop", "true");
            CropIntent.putExtra("outputX", 1024);
            CropIntent.putExtra("outputY", 1024);
            CropIntent.putExtra("return-data", true);
            CropIntent.putExtra("return-uri", uri.toString());
            startActivityForResult(CropIntent, 222);
        }catch (ActivityNotFoundException e) {
        }
    }

Android does not have a CROP Intent . Android 没有CROP Intent There is no requirement for any device to support that undocumented Intent , let alone with those undocumented extras.没有任何设备需要支持未记录的Intent ,更不用说那些未记录的附加功能了。 There are dozens of libraries for image cropping .几十个图像裁剪库 Please use one.请使用一个。

What to use使用什么


You can use uCrop library.您可以使用uCrop库。

Implemention实施


Make sure you have this line in your settings.gradle确保你的settings.gradle中有这一行。gradle

maven { url "https://jitpack.io" }

Add it to your build.gradle .将其添加到您的build.gradle

implementation 'com.github.yalantis:ucrop:2.2.6'

Then you should add it to your manifest然后你应该将它添加到你的清单中

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

How to use如何使用


You can crop a image like this您可以像这样裁剪图像

UCrop.of(yourImageUri, whereToSaveYourCroppedImageUri)
    .withAspectRatio(16, 9) // you can change the aspect ratio.
    .withMaxResultSize(maxWidth, maxHeight) // you can add a custom result height for the image. eg 512 X 512
    .start(context); // enter the context and the crop will start.

Get the result得到结果


You can fetch the result in the onActivityResult like this您可以像这样在onActivityResult中获取结果

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK && requestCode == UCrop.REQUEST_CROP) {
        final Uri resultUri = UCrop.getOutput(data);
        // crop is successful
    } else if (resultCode == UCrop.RESULT_ERROR) {
        final Throwable cropError = UCrop.getError(data);
        // crop failed
    }
}

Output Output


View the output gif from here 从这里查看 output gif

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM