Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added SaveType #25

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +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)
.start(this);
```

Expand Down
17 changes: 17 additions & 0 deletions cutout/src/main/java/com/github/gabrielbb/cutout/CutOut.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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() {

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.github.gabrielbb.cutout;

public enum CutOutSaveTypes {

SAVE_TO_CACHE,
SAVE_TO_STORAGE


}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,9 +26,11 @@ class SaveDrawingTask extends AsyncTask<Bitmap, Void, Pair<File, Exception>> {
private static final String SAVED_IMAGE_NAME = "cutout_tmp";

private final WeakReference<CutOutActivity> activityWeakReference;
private CutOutSaveTypes type;

SaveDrawingTask(CutOutActivity activity) {
SaveDrawingTask(CutOutActivity activity, CutOutSaveTypes type) {
this.activityWeakReference = new WeakReference<>(activity);
this.type = type;
}

@Override
Expand All @@ -35,16 +41,24 @@ protected void onPreExecute() {

@Override
protected Pair<File, Exception> 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());
}
}

Expand All @@ -64,4 +78,52 @@ protected void onPostExecute(Pair<File, Exception> 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;
}
}
}
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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