如何在图库容器中添加向左、向右、顶部、底部和裁剪选项



我使用内部存储来保存图像并在网格视图中检索,现在我的任务是在库中打开带有旋转和裁剪选项的图像,我的示例在Android棒棒糖版本中打开库中的图像,其中包含旋转和裁剪选项,但在Android oreo版本中,图像在库中开放,但没有显示任何选项。下面给出的示例代码将在库视图中打开。

Intent intent = new Intent(Intent.ACTION_VIEW)//
.setDataAndType(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N ?
android.support.v4.content.FileProvider.getUriForFile(MyFileActivity.this,getPackageName() + ".provider", file) : Uri.fromFile(file),
"image/*").addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivityForResult(intent,PICK_IMAGE_REQUEST);

使用此库,您可以裁剪、旋转左、右、上、下图库图像

将此库添加到您的build.gradle(Module.app(文件中

implementation 'com.theartofdev.edmodo:android-image-cropper:2.7.0'

将此行添加到Proguard配置文件

-keep class android.support.v7.widget.** { *; }

将CropImageActivity添加到AndroidManifest.xml 中

<activity android:name="com.theartofdev.edmodo.cropper.CropImageActivity"
android:theme="@style/Base.Theme.AppCompat"/> <!-- optional (needed if 
default theme has no action bar) -->

并且在onClick图库或相机按钮中添加以下方法

CropImage.activity()                                      
.setGuidelines(CropImageView.Guidelines.ON)
.setAspectRatio(1,1)
.start(YourActivityName.this);
// for fragment (DO NOT use `getActivity()`)
CropImage.activity()
.start(getContext(), this);

现在在你的活动中的onActivityResult方法中获取作物结果

@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) {
Uri resultUri = result.getUri();
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) 
{
Exception error = result.getError();
}
}
}

最新更新