From dc29a05bf3e3ff6559e919891539ef39719ea7af Mon Sep 17 00:00:00 2001 From: amir shiati Date: Sat, 26 Dec 2020 23:05:58 +0330 Subject: [PATCH 1/3] added save types --- .../com/github/gabrielbb/cutout/CutOut.java | 17 ++++ .../gabrielbb/cutout/CutOutActivity.java | 4 +- .../gabrielbb/cutout/CutOutSaveTypes.java | 8 ++ .../gabrielbb/cutout/SaveDrawingTask.java | 82 ++++++++++++++++--- gradle/wrapper/gradle-wrapper.properties | 2 +- 5 files changed, 101 insertions(+), 12 deletions(-) create mode 100644 cutout/src/main/java/com/github/gabrielbb/cutout/CutOutSaveTypes.java diff --git a/cutout/src/main/java/com/github/gabrielbb/cutout/CutOut.java b/cutout/src/main/java/com/github/gabrielbb/cutout/CutOut.java index 7c169e4..d0d6366 100644 --- a/cutout/src/main/java/com/github/gabrielbb/cutout/CutOut.java +++ b/cutout/src/main/java/com/github/gabrielbb/cutout/CutOut.java @@ -18,6 +18,7 @@ public class CutOut { static final String CUTOUT_EXTRA_BORDER_COLOR = "CUTOUT_EXTRA_BORDER_COLOR"; static final String CUTOUT_EXTRA_CROP = "CUTOUT_EXTRA_CROP"; static final String CUTOUT_EXTRA_INTRO = "CUTOUT_EXTRA_INTRO"; + static final String CUTOUT_EXTRA_SAVE_TYPE = "CUTOUT_EXTRA_SAVE_TYPE"; public static ActivityBuilder activity() { return new ActivityBuilder(); @@ -34,6 +35,7 @@ public static final class ActivityBuilder { private boolean crop = true; // By default the cropping activity is started private boolean intro; private int borderColor = Color.WHITE; // Default border color is no border color is passed + private CutOutSaveTypes type; private ActivityBuilder() { @@ -62,6 +64,9 @@ private Intent getIntent(@NonNull Context context) { intent.putExtra(CUTOUT_EXTRA_INTRO, true); } + if(type != null) + intent.putExtra(CUTOUT_EXTRA_SAVE_TYPE, type); + return intent; } @@ -101,6 +106,18 @@ public ActivityBuilder noCrop() { return this; } + /** + * Set saving image type it can either be: + * Saved to cache + * Saved to storage + * @param type + * @return + */ + public ActivityBuilder saveType(CutOutSaveTypes type) { + this.type = type; + return this; + } + /** * Shows an introduction to the activity, explaining every button usage. The intro is show only once. */ diff --git a/cutout/src/main/java/com/github/gabrielbb/cutout/CutOutActivity.java b/cutout/src/main/java/com/github/gabrielbb/cutout/CutOutActivity.java index 850f9dd..a853530 100644 --- a/cutout/src/main/java/com/github/gabrielbb/cutout/CutOutActivity.java +++ b/cutout/src/main/java/com/github/gabrielbb/cutout/CutOutActivity.java @@ -38,6 +38,7 @@ import static android.view.View.INVISIBLE; import static android.view.View.VISIBLE; import static com.github.gabrielbb.cutout.CutOut.CUTOUT_EXTRA_INTRO; +import static com.github.gabrielbb.cutout.CutOut.CUTOUT_EXTRA_SAVE_TYPE; public class CutOutActivity extends AppCompatActivity { @@ -205,7 +206,8 @@ private void start() { } private void startSaveDrawingTask() { - SaveDrawingTask task = new SaveDrawingTask(this); + CutOutSaveTypes type = (CutOutSaveTypes) getIntent().getSerializableExtra(CUTOUT_EXTRA_SAVE_TYPE); + SaveDrawingTask task = new SaveDrawingTask(this , type); int borderColor; if ((borderColor = getIntent().getIntExtra(CutOut.CUTOUT_EXTRA_BORDER_COLOR, -1)) != -1) { diff --git a/cutout/src/main/java/com/github/gabrielbb/cutout/CutOutSaveTypes.java b/cutout/src/main/java/com/github/gabrielbb/cutout/CutOutSaveTypes.java new file mode 100644 index 0000000..703e243 --- /dev/null +++ b/cutout/src/main/java/com/github/gabrielbb/cutout/CutOutSaveTypes.java @@ -0,0 +1,8 @@ +package com.github.gabrielbb.cutout; + +public enum CutOutSaveTypes { + + SAVE_TO_CACHE, + SAVE_TO_STORAGE + +} diff --git a/cutout/src/main/java/com/github/gabrielbb/cutout/SaveDrawingTask.java b/cutout/src/main/java/com/github/gabrielbb/cutout/SaveDrawingTask.java index 6907baa..2f73483 100644 --- a/cutout/src/main/java/com/github/gabrielbb/cutout/SaveDrawingTask.java +++ b/cutout/src/main/java/com/github/gabrielbb/cutout/SaveDrawingTask.java @@ -6,12 +6,16 @@ import android.net.Uri; import android.os.AsyncTask; import android.os.Environment; +import android.util.Log; import android.util.Pair; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.lang.ref.WeakReference; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Locale; import java.util.UUID; import static android.view.View.VISIBLE; @@ -22,9 +26,11 @@ class SaveDrawingTask extends AsyncTask> { private static final String SAVED_IMAGE_NAME = "cutout_tmp"; private final WeakReference activityWeakReference; + private CutOutSaveTypes type; - SaveDrawingTask(CutOutActivity activity) { + SaveDrawingTask(CutOutActivity activity, CutOutSaveTypes type) { this.activityWeakReference = new WeakReference<>(activity); + this.type = type; } @Override @@ -35,16 +41,24 @@ protected void onPreExecute() { @Override protected Pair doInBackground(Bitmap... bitmaps) { - - try { - File file = File.createTempFile(SAVED_IMAGE_NAME, SAVED_IMAGE_FORMAT, activityWeakReference.get().getApplicationContext().getCacheDir()); - - try (FileOutputStream out = new FileOutputStream(file)) { - bitmaps[0].compress(Bitmap.CompressFormat.PNG, 95, out); - return new Pair<>(file, null); + if (type == CutOutSaveTypes.SAVE_TO_CACHE) { + try { + File file = File.createTempFile(SAVED_IMAGE_NAME, SAVED_IMAGE_FORMAT, activityWeakReference.get().getApplicationContext().getCacheDir()); + + try (FileOutputStream out = new FileOutputStream(file)) { + bitmaps[0].compress(Bitmap.CompressFormat.PNG, 95, out); + return new Pair<>(file, null); + } + } catch (IOException e) { + return new Pair<>(null, e); } - } catch (IOException e) { - return new Pair<>(null, e); + } else { + SaveToStorageResult result = saveToStorage(bitmaps[0]); + File file = new File(result.getPath()); + if (file.exists()) + return new Pair<>(file, null); + else + return new Pair<>(null, result.getException()); } } @@ -64,4 +78,52 @@ protected void onPostExecute(Pair result) { activityWeakReference.get().exitWithError(result.second); } } + + private SaveToStorageResult saveToStorage(Bitmap capturedBitmap) { + File folder = new File(Environment.getExternalStorageDirectory() + + File.separator + "cutout"); + + folder.mkdirs(); + + SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HH_mm_SS", Locale.US); + + String format = sdf.format(new Date()); + + File photoFile = new File(folder, format.concat(".png")); + + if (photoFile.exists()) { + photoFile.delete(); + } + + try { + FileOutputStream fos = new FileOutputStream(photoFile.getPath()); + + capturedBitmap.compress(Bitmap.CompressFormat.PNG, 60, fos); + + fos.flush(); + fos.close(); + return new SaveToStorageResult(photoFile.getAbsolutePath(), null); + } catch (IOException e) { + Log.e("Cutout", "Exception in photoCallback", e); + return new SaveToStorageResult("", e); + } + } + + public class SaveToStorageResult { + private String path; + private IOException exception; + + public SaveToStorageResult(String path, IOException exception) { + this.path = path; + this.exception = exception; + } + + public String getPath() { + return path; + } + + public IOException getException() { + return exception; + } + } } \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index efac220..d0c1df8 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip From 7ff1a25de790496ccfb7a90805e301aa8f69b101 Mon Sep 17 00:00:00 2001 From: amir shiati Date: Sat, 26 Dec 2020 23:09:53 +0330 Subject: [PATCH 2/3] updated README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 22311b2..08d44fc 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,7 @@ You can use one or more options from these: .bordered() .noCrop() .intro() + .saveType(CutOutSaveTypes.SAVE_TO_STORAGE) .start(this); ``` From d6c985216991c689836ae40431713cbe4ce40645 Mon Sep 17 00:00:00 2001 From: amir shiati Date: Sat, 26 Dec 2020 23:14:42 +0330 Subject: [PATCH 3/3] updated README --- README.md | 4 ++-- .../java/com/github/gabrielbb/cutout/CutOutSaveTypes.java | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 08d44fc..d2182a1 100644 --- a/README.md +++ b/README.md @@ -57,12 +57,12 @@ CutOut.activity().start(this); You can use one or more options from these: ```java - CutOut.activity() + CutOut.activity() .src(uri) .bordered() .noCrop() .intro() - .saveType(CutOutSaveTypes.SAVE_TO_STORAGE) + .saveType(CutOutSaveTypes.SAVE_TO_STORAGE) .start(this); ``` diff --git a/cutout/src/main/java/com/github/gabrielbb/cutout/CutOutSaveTypes.java b/cutout/src/main/java/com/github/gabrielbb/cutout/CutOutSaveTypes.java index 703e243..df5bf6c 100644 --- a/cutout/src/main/java/com/github/gabrielbb/cutout/CutOutSaveTypes.java +++ b/cutout/src/main/java/com/github/gabrielbb/cutout/CutOutSaveTypes.java @@ -5,4 +5,5 @@ public enum CutOutSaveTypes { SAVE_TO_CACHE, SAVE_TO_STORAGE + }