当前位置: 首页 > 工具软件 > Image Cropper > 使用案例 >

android使用java添加图片预览压缩库Android Image Cropper

曾新
2023-12-01

图片预览功能介绍
Step 1. Add the JitPack repository to your root build.gradle

 allprojects {
     repositories {
       ....
       maven { url 'https://jitpack.io' }
     }
  }

Step 2. Add the dependency

dependencies {
       implementation 'com.github.CanHub:Android-Image-Cropper:3.2.1'
  }

Step 3. Add permissions to manifest

<manifest>
   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
       android:maxSdkVersion="28" />
</manifest>

Step 4. Set source compatibility version to Java 8

compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

Calling crop directly

CropImageContract contract = new CropImageContract();
        CropImageOptions options = new CropImageOptions();
        CropImageContractOptions contractOptions = new CropImageContractOptions(null, options);
        contractOptions.setGuidelines(CropImageView.Guidelines.ON).setFixAspectRatio(true);
        startActivityForResult(contract.createIntent(getActivity(), contractOptions),CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE);
@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        String msg = "onActivityResult";
        if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
            msg += " 203";
            CropImage.ActivityResult result = CropImage.getActivityResult(data);
            if (resultCode == Activity.RESULT_OK) {
//                Uri resultUri = result.getUriContent();
                String filePath = result.getUriFilePath(getActivity(),true);
                msg += " RESULT_OK " + filePath;
                mProfileLayout.updateUserIcon(filePath);
            } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
                Exception error = result.getError();
                msg += " 204 " + error.getMessage() + ";" + result.getSampleSize() + ";" + result.getUriContent();
                ToastUtils.showShort(error.getMessage());
            }
        }
        ToastUtils.showLong(msg);
    }

这里注意一点就是对文件的获取

/**
         * The file path of the image to load
         * Null if get cropped image was executed, no output requested or failure.
         *
         * @param context used to access Android APIs, like content resolve, it is your
         * activity/fragment/widget.
         * @param uniqueName If true, make each image cropped have a different file name, this could
         * cause memory issues, use wisely. [Default: false]
         */
 类似资料: