Skip to content

Commit

Permalink
added a better saving method
Browse files Browse the repository at this point in the history
  • Loading branch information
r1walz committed Mar 23, 2018
1 parent 53ad195 commit d4d56ce
Showing 1 changed file with 29 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
Expand All @@ -26,7 +28,14 @@
import com.sdsmdg.kd.trianglify.models.Palette;
import com.sdsmdg.kd.trianglify.views.TrianglifyView;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Random;

public class MainActivity extends AppCompatActivity {
Expand Down Expand Up @@ -225,7 +234,10 @@ public int pxToDp(int px) {
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_save:
exportImage();
try {
exportImage();
} catch (IOException ignored) {
}
break;
case R.id.action_about:
Intent aboutActivityIntent = new Intent(this, AboutActivity.class);
Expand Down Expand Up @@ -268,7 +280,7 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
}
}

private void exportImage() {
private void exportImage() throws IOException {
// Checks if permission is required for android version > 6
boolean permissionStatus = ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED;
Expand All @@ -279,18 +291,22 @@ private void exportImage() {
PERMISSION_CODE);
} else {
Bitmap bitmap = trianglifyView.getBitmap();
if (bitmap != null) {
if (bitmap != null)
addImageToGallery(bitmap, this);
Toast.makeText(this, "Saved!", Toast.LENGTH_SHORT).show();
} else {
else
Toast.makeText(this, "Unable to generate image, please try again",
Toast.LENGTH_LONG).show();
}

}
}

public static void addImageToGallery(Bitmap bitmap, Context context) {
MediaStore.Images.Media.insertImage(context.getContentResolver(), bitmap, "", "");
public static void addImageToGallery(Bitmap bitmap, Context context) throws IOException {
String timeStamp = "IMG_" + new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date()) + ".png";
OutputStream os = new FileOutputStream(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + File.separator + timeStamp);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
os.flush();
os.close();
Toast.makeText(context, "Saved!", Toast.LENGTH_SHORT).show();
}

// Sets bitmap from trianglify view as wallpaper of device
Expand Down Expand Up @@ -328,7 +344,11 @@ public void onRequestPermissionsResult(int requestCode,
case PERMISSION_CODE:
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
exportImage();
try {
exportImage();
} catch (IOException ignored) {

}
} else {
Toast.makeText(this, "Storage access failed, check permission",
Toast.LENGTH_LONG).show();
Expand Down

0 comments on commit d4d56ce

Please sign in to comment.