java 基于Rxjava的RImagePicker可以在您的项目中快速简单地实现

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 基于Rxjava的RImagePicker可以在您的项目中快速简单地实现相关的知识,希望对你有一定的参考价值。

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.net.Uri;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import com.mlsdev.rximagepicker.RxImageConverters;
import com.mlsdev.rximagepicker.RxImagePicker;
import com.mlsdev.rximagepicker.Sources;

import java.io.File;
import java.util.Date;

import io.reactivex.ObservableSource;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;

/**
 * To use this picker
 * please add implementation 'com.mlsdev.rximagepicker:library:2.0.2'
 */

public class RImagePicker implements DialogInterface.OnClickListener {

    private String[] options = {
            "Gallery", "Camera"
    };

    private AlertDialog.Builder builder;
    private AlertDialog alertDialog;

    private Context context;
    private IPickerListener listener;
    private String anyThing;

    private RxImagePicker rxImagePicker;
    private Disposable subscribe;

    public RImagePicker(Context context) {
        this.context = context;
        this.rxImagePicker = RxImagePicker.with(context);
        builder = new AlertDialog.Builder(context);
    }

    public RImagePicker listener(IPickerListener listener) {
        this.listener = listener;
        return this;
    }

    public RImagePicker subscribe() {
        builder.setItems(options, this);
        alertDialog = builder.create();

        return this;
    }

    public void show(@Nullable String anyThing) {
        this.anyThing = anyThing;
        if (alertDialog != null)
            alertDialog.show();
        else
            Toast.makeText(context, "Please call the 'subscribe() before show()'", Toast.LENGTH_SHORT).show();
    }

    @SuppressLint("CheckResult")
    @Override
    public void onClick(DialogInterface dialog, int which) {
        if (!((AppCompatActivity) context).isFinishing()) {
            if (listener != null) {
                if (which == 0) {
                    rxImagePicker.requestImage(Sources.GALLERY);
                } else {
                    rxImagePicker.requestImage(Sources.CAMERA);
                }
                subscribe = rxImagePicker.getActiveSubscription()
                        .flatMap(new Function<Uri, ObservableSource<File>>() {
                            @Override
                            public ObservableSource<File> apply(@NonNull Uri uri) throws Exception {
                                return RxImageConverters.uriToFile(
                                        context,
                                        uri,
                                        new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), "IMG_" + new Date().toString())
                                );
                            }
                        })
                        .subscribe(new io.reactivex.functions.Consumer<File>() {
                            @Override
                            public void accept(@NonNull File file) throws Exception {
                                listener.onPick(file, anyThing);
                            }
                        });
            } else {
                Toast.makeText(context, "Please attach the 'IPickerListener'", Toast.LENGTH_SHORT).show();
            }
        }
    }

    public void unsubscribe() {
        if (subscribe != null && !subscribe.isDisposed()) {
            subscribe.dispose();
        }
    }

    public interface IPickerListener {
        void onPick(File file, @Nullable String yourAnyThing);
    }
}

以上是关于java 基于Rxjava的RImagePicker可以在您的项目中快速简单地实现的主要内容,如果未能解决你的问题,请参考以下文章

RxJava2.0的使用详解

android Retrofig RxJava写法基于Kotlin在优化

RxJava学习小结

RxJava开发精要1-从.NET到RxJava

基于Vert.x和RxJava 2构建通用的爬虫框架

RxJava系列2(基本概念及使用介绍)