From b7d86c34da59de402ebfe34c2a8fbf36cc6c8fd2 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Thu, 19 Jan 2017 11:46:00 +0100 Subject: [PATCH] Use the selected extension when saving --- .../com/simplemobiletools/draw/MyCanvas.java | 4 ++ .../java/com/simplemobiletools/draw/Svg.java | 5 +- .../draw/activities/MainActivity.java | 59 ++++++++++++------- 3 files changed, 45 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/com/simplemobiletools/draw/MyCanvas.java b/app/src/main/java/com/simplemobiletools/draw/MyCanvas.java index 4b1f2bc..0fc70bb 100644 --- a/app/src/main/java/com/simplemobiletools/draw/MyCanvas.java +++ b/app/src/main/java/com/simplemobiletools/draw/MyCanvas.java @@ -94,6 +94,10 @@ public class MyCanvas extends View { return bitmap; } + public Map getPaths() { + return mPaths; + } + @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); diff --git a/app/src/main/java/com/simplemobiletools/draw/Svg.java b/app/src/main/java/com/simplemobiletools/draw/Svg.java index 13aadc3..2db2f58 100644 --- a/app/src/main/java/com/simplemobiletools/draw/Svg.java +++ b/app/src/main/java/com/simplemobiletools/draw/Svg.java @@ -12,12 +12,11 @@ import java.util.Map; public class Svg { - public static boolean saveSvg(File output, - Map paths, int width, int height) { + public static boolean saveSvg(File output, MyCanvas canvas) { try { FileOutputStream out = new FileOutputStream(output); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out)); - writeSvg(writer, paths, width, height); + writeSvg(writer, canvas.getPaths(), canvas.getWidth(), canvas.getHeight()); writer.close(); return true; } catch (IOException e) { diff --git a/app/src/main/java/com/simplemobiletools/draw/activities/MainActivity.java b/app/src/main/java/com/simplemobiletools/draw/activities/MainActivity.java index ca4069f..d962df2 100644 --- a/app/src/main/java/com/simplemobiletools/draw/activities/MainActivity.java +++ b/app/src/main/java/com/simplemobiletools/draw/activities/MainActivity.java @@ -20,11 +20,13 @@ import android.view.View; import android.widget.EditText; import android.widget.ImageView; import android.widget.SeekBar; +import android.widget.Spinner; import android.widget.Toast; import com.simplemobiletools.draw.Config; import com.simplemobiletools.draw.MyCanvas; import com.simplemobiletools.draw.R; +import com.simplemobiletools.draw.Svg; import com.simplemobiletools.draw.Utils; import java.io.ByteArrayOutputStream; @@ -163,6 +165,8 @@ public class MainActivity extends SimpleActivity implements MyCanvas.PathsChange final EditText fileNameET = (EditText) saveFileView.findViewById(R.id.file_name); fileNameET.setText(curFileName); + + final Spinner fileExtensionS = (Spinner) saveFileView.findViewById(R.id.file_extension); builder.setView(saveFileView); builder.setPositiveButton(R.string.ok, null); @@ -174,9 +178,9 @@ public class MainActivity extends SimpleActivity implements MyCanvas.PathsChange @Override public void onClick(View v) { final String fileName = fileNameET.getText().toString().trim(); - + final String extension = (String) fileExtensionS.getSelectedItem(); if (!fileName.isEmpty()) { - if (saveFile(fileName + ".png")) { + if (saveFile(fileName, extension)) { curFileName = fileName; Utils.showToast(getApplicationContext(), R.string.saving_ok); alertDialog.dismiss(); @@ -190,7 +194,7 @@ public class MainActivity extends SimpleActivity implements MyCanvas.PathsChange }); } - private boolean saveFile(final String fileName) { + private boolean saveFile(final String fileName, final String extension) { final String path = Environment.getExternalStorageDirectory().toString(); final File directory = new File(path, SAVE_FOLDER_NAME); if (!directory.exists()) { @@ -199,24 +203,39 @@ public class MainActivity extends SimpleActivity implements MyCanvas.PathsChange } } - final Bitmap bitmap = mMyCanvas.getBitmap(); - FileOutputStream out = null; - try { - final File file = new File(directory, fileName); - out = new FileOutputStream(file); - bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); - MediaScannerConnection.scanFile(getApplicationContext(), new String[]{file.getAbsolutePath()}, null, null); - } catch (Exception e) { - Log.e(TAG, "MainActivity SaveFile " + e.getMessage()); - return false; - } finally { - try { - if (out != null) { - out.close(); + final File file = new File(directory, fileName+extension); + switch (extension) { + case ".png": + final Bitmap bitmap = mMyCanvas.getBitmap(); + FileOutputStream out = null; + try { + out = new FileOutputStream(file); + bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); + MediaScannerConnection.scanFile(getApplicationContext(), + new String[]{file.getAbsolutePath()}, null, null); + } catch (Exception e) { + Log.e(TAG, "MainActivity SaveFile " + e.getMessage()); + return false; + } finally { + try { + if (out != null) { + out.close(); + } + } catch (IOException e) { + Log.e(TAG, "MainActivity SaveFile 2 " + e.getMessage()); + } } - } catch (IOException e) { - Log.e(TAG, "MainActivity SaveFile 2 " + e.getMessage()); - } + break; + + case ".svg": + if (!Svg.saveSvg(file, mMyCanvas)) { + Log.e(TAG, "MainActivity SaveFile failed."); + return false; + } + break; + + default: + return false; } return true;