RxGallery


Source link: https://github.com/marchinram/RxGallery

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.0' 
}
 

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 {

 
}

  
}
);

  
}

 ... 
}
 

Resources

Material progress circle to wrap around any FloatingActionButton. 100% Material Design guidelines from Google.

Mara is a library to provide traits or composition capabilities to your Android or Java projects.

Material design ProgressBar with consistent appearance on Android 4.0+.

Material image loading implementation.

Android Debug Drawer for faster development.

Simple "pull-to-update" layout.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes