• Tamic/original

    RxJava for Java and Android developers have been familiar with the technology, RxJava step communication technology is favored by developers, this article will bring a RxJava through the RxJava to choose mobile phone album to obtain multimedia tools RxFile.

Tamic ‘

RxFile is a tool for retrieving images and videos from Android devices. It makes it easier for you to retrieve images from albums and prevent ANR. RxFile is written by Tournaris, and this article begins by explaining how to use RxFile.

integration

Add the following dependencies to Gradle jCenter() and mavenCentral() :

The compile ‘com. Making. Pavlospt: rxfile: 1.5’

use

1 Enable the LOG function

RxFile.setLoggingEnabled(true);

2 Get multiple files from ClipData

RxFile.createFilesFromClipData(this,clipData)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<List<File>>() {                    
                    @Override
                    public void onCompleted() {                        
                      Timber.e("onCompleted() for Files called");
                    }                    
                    @Override
                    public void onError(Throwable e) {                        
                      Timber.e("Error on files fetching:" + e.getMessage());
                    }                    
                    @Override
                    public void onNext(List<File> files) {                        
                     Timber.e("Files list size:" + files.size());                        
                     for(File f : files){                            
                        Timber.e("onNext() file called:"+ f.getAbsolutePath()); }}});Copy the code

3 Configure the media file by URI

RxFile.createFileFromUri(this,uri)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<File>() {                    @Override
                    public void onCompleted() {                        Timber.e("onCompleted() for File called");
                    }                    
                    @Override
                    public void onError(Throwable e) {                        Timber.e("Error on file fetching:" + e.getMessage());
                    }                    
                    @Override
                    public void onNext(File file) {                        Timber.e("onNext() file called:"+ file.getAbsolutePath()); }});Copy the code

Thumbnails files get really big over time because they can cause your camera to fail to take a picture due to lack of storage space or bluetooth to fail to transfer files. Sometimes we need to read this file to do something.

RxFile.getThumbnail(this,data)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<Bitmap>() {                   
                    @Override
                    public void onCompleted() {                        
                       Timber.e("onCompleted() called");
                    }                    
                    @Override
                    public void onError(Throwable e) {                        
                       Timber.e("onError called with: "+ e.getMessage()); } @Override public void onNext(Bitmap bitmap) { mBitmap.setImageBitmap(bitmap); }}); `Copy the code

More API

Here are some of the top callable methods that RXFile provides to get different file formats from different sources, making it easier for you to face the need for photo albums and video selections.

Observable getFileExtension(final String fileName);

Observable ifExists(final String path);

Observable getThumbnail(String filePath);

Observable getVideoThumbnail(final String filePath);

Observable getVideoThumbnailFromPath(final String path, final int kind);

Observable getThumbnailFromPath(String filePath);

Observable getFileType(String filePath);

Observable getPathFromUriForFileDocument(final Context context, final Uri contentUri);

Observable getPathFromUriForImageDocument(final Context context, final String mediaDocumentId);

Observable getPathFromUriForVideoDocument(final Context context, final String mediaDocumentId);

String getMimeType(String fileName);

The end of the

RxFile is a very light framework, with only 616 lines of code, so you don’t need to worry about the package size when you develop it. It can also solve the phenomenon of stuck or Anr caused by reading pictures. RxFIle principle is very simple, basic introduction to RxJava can write their own RxFIle.

Project address: github.com/pavlospt/Rx…

More articles can be wechat search attention to my public number: developer technology front