aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/SaveBitmapImageToFile.java
blob: c5388f087f5cbcd0826e0003ea6bdeffe187911a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package ml.docilealligator.infinityforreddit.asynctasks;

import android.graphics.Bitmap;
import android.os.Handler;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.concurrent.Executor;

public class SaveBitmapImageToFile {

    public static void SaveBitmapImageToFile(Executor executor, Handler handler, Bitmap resource, String cacheDirPath, String fileName,
                                             SaveBitmapImageToFileListener saveBitmapImageToFileListener) {
        executor.execute(() -> {
            try {
                File imageFile = new File(cacheDirPath, fileName);
                OutputStream outputStream = new FileOutputStream(imageFile);
                resource.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
                outputStream.flush();
                outputStream.close();

                handler.post(() -> saveBitmapImageToFileListener.saveSuccess(imageFile));
            } catch (IOException e) {
                handler.post(saveBitmapImageToFileListener::saveFailed);
            }
        });
    }

    public interface SaveBitmapImageToFileListener {
        void saveSuccess(File imageFile);
        void saveFailed();
    }
}