package by.vshkl.pxlsrt.core.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.FileOutputStream;
import java.io.IOException;

import by.vshkl.pxlsrt.mvp.presenter.CameraPresenter;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class BitmapUtils {

    public static Observable<String> saveTempBitmap(final FileOutputStream fos, final String fname, final byte[] data) {
        return Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(final ObservableEmitter<String> emitter) throws Exception {
                Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
                bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getWidth());
                bitmap = Bitmap.createScaledBitmap(
                        bitmap, CameraPresenter.IMAGE_SIZE_PX, CameraPresenter.IMAGE_SIZE_PX, false);
                storeFile(fos, bitmap)
                        .subscribeOn(Schedulers.io())
                        .subscribe(new Consumer<Boolean>() {
                            @Override
                            public void accept(Boolean aBoolean) throws Exception {
                                if (aBoolean) {
                                    emitter.onNext(fname);
                                }
                            }
                        });
            }
        });
    }

    static Observable<Boolean> storeFile(final FileOutputStream fos, final Bitmap bitmap) {
        return Observable.create(new ObservableOnSubscribe<Boolean>() {
            @Override
            public void subscribe(ObservableEmitter<Boolean> emitter) throws Exception {
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
                bitmap.recycle();
                try {
                    fos.close();
                    emitter.onNext(true);
                } catch (IOException e) {
                    e.printStackTrace();
                    emitter.onError(e);
                }
            }
        });
    }
}

@@ -102,16 +102,48 @@
 ctory;%0A%0A
+import java.io.FileInputStream;%0A
 import j
@@ -1629,16 +1629,617 @@
 %0A    %7D%0A%0A
+    public static Observable%3CBitmap%3E getStoredBitmap(final FileInputStream fis) %7B%0A        return Observable.create(new ObservableOnSubscribe%3CBitmap%3E() %7B%0A            @Override%0A            public void subscribe(ObservableEmitter%3CBitmap%3E emitter) throws Exception %7B%0A                Bitmap bitmap = BitmapFactory.decodeStream(fis);%0A                try %7B%0A                    fis.close();%0A                    emitter.onNext(bitmap);%0A                %7D catch (IOException e) %7B%0A                    e.printStackTrace();%0A                    emitter.onError(e);%0A                %7D%0A            %7D%0A        %7D);%0A    %7D%0A%0A
     stat

Update BitmapUtils. Add getStoredBitmap method