RxGallery

Additional

Language
Java
Version
N/A
Created
Jul 28, 2017
Updated
Feb 20, 2019 (Retired)
Owner
Brian Rojas (marchinram)
Contributor
Brian Rojas (marchinram)
1
Activity
Badge
Generate
Download
Source code

RxGallery

Android gallery & photo/video functionality simplified with RxJava2

Setup

To use this library your minSdkVersion must be >= 9.

Add it in your root build.gradle at the end of repositories:

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

Add the dependency

dependencies {
  compile 'com.github.marchinram:RxGallery:0.6.6'
}

Usage

Picking items from the gallery

Maybe<List<Uri>> RxGallery.gallery(@NonNull Activity activity)
Maybe<List<Uri>> RxGallery.gallery(@NonNull Activity activity, boolean multiSelectEnabled)
Maybe<List<Uri>> RxGallery.gallery(@NonNull Activity activity, boolean multiSelectEnabled, @Nullable MimeType... mimeTypes)

Example - Picking multiple images/videos from the gallery:

RxGallery.gallery(this, true, RxGallery.MimeType.IMAGE, RxGallery.MimeType.VIDEO).subscribe(new Consumer<List<Uri>>() {
    @Override
    public void accept(List<Uri> uris) throws Exception {
        doStuffWithUris(uris);
    }
}, new Consumer<Throwable>() {
    @Override
    public void accept(Throwable throwable) throws Exception {
        Toast.makeText(SomeActivity.this, throwable.getMessage(), Toast.LENGTH_LONG).show();
    }
});

Taking photos with camera

Maybe<Uri> RxGallery.photoCapture(@NonNull Activity activity)
Maybe<Uri> RxGallery.photoCapture(@NonNull Activity activity, @Nullable Uri outputUri)

Example - Taking a photo with the camera and saving it to gallery:

RxGallery.photoCapture(this).subscribe(new Consumer<Uri>() {
    @Override
    public void accept(Uri uri) throws Exception {
        doStuffWithUri(uri);
    }
}, new Consumer<Throwable>() {
    @Override
    public void accept(Throwable throwable) throws Exception {
        Toast.makeText(SomeActivity.this, throwable.getMessage(), Toast.LENGTH_LONG).show();
    }
});

In 6.0+ you need to ask for WRITE_EXTERNAL_STORAGE permission to save to gallery, below is an example doing this with RxPermissions and flatMap:

Observable<Boolean> permissionObservable = Observable.just(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    permissionObservable = new RxPermissions(this).request(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}

permissionObservable.flatMap(new Function<Boolean, ObservableSource<Uri>>() {
    @Override
    public ObservableSource<Uri> apply(@NonNull Boolean granted) throws Exception {
        if (!granted) {
            return Observable.empty();
        }
        return RxGallery.photoCapture(SomeActivity.this).toObservable();
    }
}).subscribe(new Consumer<Uri>() {
    @Override
    public void accept(Uri uri) throws Exception {
        doStuffWithUri(uri);
    }
}, new Consumer<Throwable>() {
    @Override
    public void accept(Throwable throwable) throws Exception {
        Toast.makeText(SomeActivity.this, throwable.getMessage(), Toast.LENGTH_LONG).show();
    }
});

Taking videos with camera

Maybe<Uri> RxGallery.videoCapture(@NonNull Activity activity)

Example - Taking a video with the camera and saving it to gallery:

RxGallery.videoCapture(this).subscribe(new Consumer<Uri>() {
    @Override
    public void accept(Uri uri) throws Exception {
        doStuffWithUri(uri);
}, new Consumer<Throwable>() {
    @Override
    public void accept(Throwable throwable) throws Exception {
        Toast.makeText(MainActivity.this, throwable.getMessage(), Toast.LENGTH_LONG).show();
    }
});

Important

If you want the started Activity (gallery/photo/video) to be destroyed when the Activity which started it is destroyed you must keep a reference to the Disposable and call dispose as shown below:

public final class SomeActivity extends Activity {

    private Disposable disposable;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
        
        disposable = RxGallery.gallery(this).subscribe(new Consumer<List<Uri>>() {
            @Override
            public void accept(List<Uri> uris) throws Exception {
                doStuffWithUris(uris);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        disposable.dispose();
    }
    
    ...
}

Alternatively you can use RxLifecycle

public final class SomeActivity extends RxActivity {

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
        
        RxGallery.gallery(this)
                .compose(this.<List<Uri>>bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new Consumer<List<Uri>>() {
                    @Override
                    public void accept(List<Uri> uris) throws Exception {
                        doStuffWithUris(uris);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {

                    }
                });
    }
    
    ...
}