ImagePicker
ImagePicker ? Android ??????????????? Android-Image-Cropper ????????
- ?????api???????
imagePicker.startChooser(context, new ImagePicker.Callback() {
// ??????
@Override public void onPickImage(Uri imageUri) {
}
// ??????
@Override public void onCropImage(Uri imageUri) {
FrescoUtils.circle(draweeView, imageUri);
}
}
);
- ?? Fragment
public void startChooser(Activity activity, @NonNull Callback callback) public void startChooser(Fragment fragment, @NonNull Callback callback)
- ?????????? uri ? content ?????????????????????? FileNotFoundException
- ?????????????????????????
- ?? Android N+
- strings.xml ???
????
????
??????? build.gradle
compile 'com.linchaolong.android:imagepicker:${
latest.version
}
'
- ? Androidmanifest.xml ???
CropImageActivity
????????
<activity android:name="com.linchaolong.android.imagepicker.cropper.CropImageActivity"
android:theme="@style/Base.Theme.AppCompat"/>
- ? Activity ?? Fragment ???
onActivityResult
?onRequestPermissionsResult
??
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
imagePicker.onActivityResult(activity/fragment, requestCode, resultCode, data);
}
@Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
imagePicker.onRequestPermissionsResult(activity/fragment, requestCode, permissions, grantResults);
}
????
1. ???????
ImagePicker imagePicker = new ImagePicker();
// ???? imagePicker.setTitle("????");
// ???????? imagePicker.setCropImage(true);
// ??????? imagePicker.startChooser(this, new ImagePicker.Callback() {
// ??????
@Override public void onPickImage(Uri imageUri) {
}
// ??????
@Override public void onCropImage(Uri imageUri) {
draweeView.setImageURI(imageUri);
draweeView.getHierarchy().setRoundingParams(RoundingParams.asCircle());
}
// ???????
@Override public void cropConfig(CropImage.ActivityBuilder builder) {
builder
// ????????
.setMultiTouchEnabled(false)
// ????????
.setGuidelines(CropImageView.Guidelines.OFF)
// ??/??
.setCropShape(CropImageView.CropShape.RECTANGLE)
// ????????????
.setRequestedSize(960, 540)
// ???
.setAspectRatio(16, 9);
}
// ????????
@Override public void onPermissionDenied(int requestCode, String[] permissions,
int[] grantResults) {
}
}
);
??????????????? ??
2. ????????
imagePicker.startGallery(activity/fragment, callback);
3. ??
imagePicker.startCamera(activity/fragment, callback);
Callback
????????????????????
public static abstract class Callback{
/**
* ??????
* @param imageUri
*/
public abstract void onPickImage(Uri imageUri);
/**
* ??????
*
* @param imageUri
*/
public void onCropImage(Uri imageUri){
}
/**
* ??????
*/
public void cropConfig(CropImage.ActivityBuilder builder){
// ????
builder.setMultiTouchEnabled(false)
.setCropShape(CropImageView.CropShape.OVAL)
.setRequestedSize(640, 640)
.setAspectRatio(5, 5);
}
/**
* ????????
*
* @param requestCode
* @param permissions
* @param grantResults
*/
public void onPermissionDenied(int requestCode, String permissions[], int[] grantResults){
}
}
??????????? app ???????