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

图片预览功能介绍
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]
         */
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值