Skip to content

Instantly share code, notes, and snippets.

@Kolyall
Last active November 8, 2018 09:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kolyall/c59d507a12abcff926bec9dea5ee9fd8 to your computer and use it in GitHub Desktop.
Save Kolyall/c59d507a12abcff926bec9dea5ee9fd8 to your computer and use it in GitHub Desktop.
Choose image source dialog

Image source pick for activity/fragment by using Gallery or Take photo

//Presenter
public void onChangeLogoClicked() {
getRouter().openChooseSourceDialog((dialog, itemView, position, text) -> {
switch (position) {
case 0: {
openImageSource(PickImageUtils.ImageSourceType.Gallery, ViewRequestCode.LOGO);
break;
}
case 1: {
openImageSource(PickImageUtils.ImageSourceType.Camera, ViewRequestCode.LOGO);
break;
}
}
});
}
//Router
public void openChooseSourceDialog(MaterialDialog.ListCallback listCallback) {
List<String> items = new ArrayList<>();
items.add(getString(R.string.select_from_gallery));
items.add(getString(R.string.take_picture));
new com.afollestad.materialdialogs.MaterialDialog.Builder(getActivity())
.title(R.string.select_source)
.itemsCallback(listCallback)
.items(items)
.show();
}
//Presenter
private void openImageSource(PickImageUtils.ImageSourceType imageSourceType, int requestCode) {
String[] permissions={Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE};
switch (imageSourceType){
case Camera:
permissions = new String[]{Manifest.permission.CAMERA,Manifest.permission.READ_EXTERNAL_STORAGE};
break;
case Gallery:
permissions = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE};
break;
}
createAndAddSubscription(
getView().getRxPermissions()
.request(permissions)
.filter(granted -> granted)
.compose(RxNetworkUtils.builder(getView()).async().progressBar().errorToast().build())
.doOnNext(granted -> getRouter().openImageSource(imageSourceType, requestCode))
);
}
//Router
public void openImageSource(ImageSourceView.ImageSourceType imageSourceType, int viewRequester) {
getActivity().startActivityForResult(PickImageUtils.getPickImageChooserIntent(getActivity(), imageSourceType), viewRequester);
}
//Activity / Fragment
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == UCrop.RESULT_ERROR) {
final Throwable cropError = UCrop.getError(data);
Log.e(TAG, "onActivityResult: ", cropError);
}
if (resultCode != Activity.RESULT_OK) {
return;
}
switch (requestCode) {
case ViewRequestCode.LOGO: {
FragmentActivity activity = getActivity();
if (isFinishing(activity)) return;
Uri imageUri = PickImageUtils.extractImageUri(activity, data);
UCrop.Options options = new UCrop.Options();
options.setCompressionFormat(Bitmap.CompressFormat.JPEG);
options.setCircleDimmedLayer(true);
Uri croppedTargetImageUri = PickImageUtils.getCaptureImageOutputUri(activity, "croppedImage.jpeg");
UCrop.of(imageUri, croppedTargetImageUri)
.withAspectRatio(1, 1)
.withMaxResultSize(500, 500)
.withOptions(options)
.start(activity, ViewRequestCode.CROP_LOGO);
break;
}
case ViewRequestCode.CROP_LOGO: {
getPresenter().onLogoCropped(UCrop.getOutput(data));
break;
}
}
}
//Presenter
public void onLogoCropped(Uri imageUri) {
getView().setUserImage(imageUri);
updateImageOnServer(imageUri);
}
//Presenter
private void updateImageOnServer(Uri imageUri) {
if (!getView().isInternetConnected()) {
getView().toastShort(R.string.not_connected_toast);
return;
}
if (imageUri != null) {
createAndAddSubscription(
mDataUserProfileRepositoryLazy.get().updateUserImage(imageUri)
.compose(RxNetworkUtils.builder(getView())
.async()
.progressBar()
.errorToast()
.build())
.doOnNext(userProfile -> {
getView().toastShort(R.string.profile_updated);
EventBus.getDefault().post(new ActionObject<>(Action.PROFILE_UPDATED,userProfile));
}));
}else{
getView().toastLong(R.string.error_image);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment