Giter Club home page Giter Club logo

smartimagepicker's Introduction

Simplified abstraction over Android Image Cropper library to pick images from gallery or camera and crop them if needed.

Usage

Add this line to build.gradle

compile 'com.myhexaville:smart-image-picker:1.0'

Create new instance and save it as field

imagePicker = new ImagePicker(this, /* activity non null*/
                null, /* fragment nullable*/
                imageUri -> {/*on image picked */
                    imageView.setImageURI(imageUri);
                })
                .setWithImageCrop(
                        1 /*aspect ratio x*/
                        1 /*aspect ratio y*/);

If calling from Activity

Override Activity's methods to delegate permissions to ImagePicker and resulting image

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    imagePicker.handleActivityResult(resultCode,requestCode, data);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    imagePicker.handlePermission(requestCode, grantResults);
}

Open Picker

There's two methods available

imagePicker.openCamera();
imagePIcker.choosePicture(true /*show camera intents*/);

First one opens camera directly, second shows an intent picker, where user picks from desired application. You can include/exclude camera intents with boolean.

That's it, if you don't need to crop image, don't call setImageCrop() in the chain. By default it's disabled. And if you want to get a file after you picked image, you can get it with this method

File file = imagePicker.getImageFiel();

If calling from Fragment

Create instance

imagePicker = new ImagePicker(getActivity(),
                this,
                imageUri -> {/*on image picked */
                    imageView.setImageURI(imageUri);
                })
                .setWithImageCrop(
                        1 /*aspect ratio x*/
                        1 /*aspect ratio y*/);

Overriden methods should be in your Fragment

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    imagePicker.handleActivityResult(resultCode, requestCode, data);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    imagePicker.handlePermission(requestCode, grantResults);
}

But your fragment won't get activity result callback itself. You need to call it manually. Add this code to activity that hosts your fragment

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    List<Fragment> fragments = getSupportFragmentManager().getFragments();
    if (fragments != null) {
        for (Fragment f : fragments) {
            if (f instanceof YourFragment) {
                f.onActivityResult(requestCode, resultCode, data);
            }
        }
    }
}

You can get a sample app here

You don't need to add any permissions to manifest, everything is merged automatically from library's manifest file

smartimagepicker's People

Contributors

ihorklimov avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.