Use the selected extension when saving

This commit is contained in:
Lonami Exo 2017-01-19 11:46:00 +01:00
parent 4b7f123a06
commit b7d86c34da
3 changed files with 45 additions and 23 deletions

View File

@ -94,6 +94,10 @@ public class MyCanvas extends View {
return bitmap;
}
public Map<MyPath,PaintOptions> getPaths() {
return mPaths;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

View File

@ -12,12 +12,11 @@ import java.util.Map;
public class Svg {
public static boolean saveSvg(File output,
Map<MyPath, PaintOptions> 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) {

View File

@ -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;