allow saving the drawing on external storage
This commit is contained in:
parent
6d5d74a158
commit
346b8f8287
|
@ -3,6 +3,8 @@
|
|||
package="com.simplemobiletools.draw"
|
||||
xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
|
||||
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
|
|
|
@ -5,12 +5,16 @@ import android.graphics.Bitmap;
|
|||
import android.graphics.Color;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.os.Environment;
|
||||
import android.provider.MediaStore;
|
||||
import android.support.v4.content.FileProvider;
|
||||
import android.support.v7.app.AlertDialog;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.util.Log;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.widget.EditText;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
|
@ -26,9 +30,11 @@ public class MainActivity extends AppCompatActivity {
|
|||
private static final String TAG = MainActivity.class.getSimpleName();
|
||||
private static final String FOLDER_NAME = "images";
|
||||
private static final String FILE_NAME = "simple-draw.png";
|
||||
private static final String SAVE_FOLDER_NAME = "Simple Draw";
|
||||
@Bind(R.id.my_canvas) MyCanvas myCanvas;
|
||||
@Bind(R.id.color_picker) View colorPicker;
|
||||
private int color;
|
||||
private String curFileName;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
|
@ -48,6 +54,9 @@ public class MainActivity extends AppCompatActivity {
|
|||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
switch (item.getItemId()) {
|
||||
case R.id.menu_save:
|
||||
saveImage();
|
||||
return true;
|
||||
case R.id.menu_share:
|
||||
shareImage();
|
||||
return true;
|
||||
|
@ -56,6 +65,73 @@ public class MainActivity extends AppCompatActivity {
|
|||
}
|
||||
}
|
||||
|
||||
private void saveImage() {
|
||||
final View saveFileView = getLayoutInflater().inflate(R.layout.save_file, null);
|
||||
|
||||
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
||||
builder.setTitle(getResources().getString(R.string.save_file));
|
||||
|
||||
final EditText fileNameET = (EditText) saveFileView.findViewById(R.id.file_name);
|
||||
fileNameET.setText(curFileName);
|
||||
builder.setView(saveFileView);
|
||||
|
||||
builder.setPositiveButton("OK", null);
|
||||
builder.setNegativeButton("Cancel", null);
|
||||
|
||||
final AlertDialog alertDialog = builder.create();
|
||||
alertDialog.show();
|
||||
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
final String fileName = fileNameET.getText().toString().trim();
|
||||
|
||||
if (!fileName.isEmpty()) {
|
||||
if (saveFile(fileName + ".png")) {
|
||||
curFileName = fileName;
|
||||
Utils.showToast(getApplicationContext(), R.string.saving_ok);
|
||||
alertDialog.dismiss();
|
||||
} else {
|
||||
Utils.showToast(getApplicationContext(), R.string.saving_error);
|
||||
}
|
||||
} else {
|
||||
Utils.showToast(getApplicationContext(), R.string.enter_file_name);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private boolean saveFile(final String fileName) {
|
||||
final String path = Environment.getExternalStorageDirectory().toString();
|
||||
final File directory = new File(path, SAVE_FOLDER_NAME);
|
||||
if (!directory.exists()) {
|
||||
if (!directory.mkdir()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
final Bitmap bitmap = myCanvas.getBitmap();
|
||||
FileOutputStream out = null;
|
||||
try {
|
||||
final File file = new File(directory, fileName);
|
||||
out = new FileOutputStream(file);
|
||||
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
|
||||
MediaStore.Images.Media.insertImage(getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName());
|
||||
} 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());
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void shareImage() {
|
||||
final String shareTitle = getResources().getString(R.string.share_via);
|
||||
final Bitmap bitmap = myCanvas.getBitmap();
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
package com.simplemobiletools.draw;
|
||||
|
||||
import android.content.Context;
|
||||
import android.widget.Toast;
|
||||
|
||||
public class Utils {
|
||||
public static void showToast(Context cxt, int msgId) {
|
||||
Toast.makeText(cxt, cxt.getResources().getString(msgId), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical"
|
||||
android:padding="@dimen/activity_margin">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/file_name_label"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/file_name"/>
|
||||
|
||||
<EditText
|
||||
android:id="@+id/file_name"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_below="@+id/file_name_label"
|
||||
android:layout_marginBottom="@dimen/activity_margin"
|
||||
android:layout_toLeftOf="@+id/file_extension"/>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/file_extension"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_alignBottom="@+id/file_name"
|
||||
android:layout_alignParentRight="true"
|
||||
android:layout_alignTop="@+id/file_name"
|
||||
android:layout_below="@+id/file_name_label"
|
||||
android:gravity="center_vertical"
|
||||
android:text=".png"/>
|
||||
|
||||
</RelativeLayout>
|
|
@ -1,6 +1,11 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
<item
|
||||
android:id="@+id/menu_save"
|
||||
android:icon="@android:drawable/ic_menu_save"
|
||||
android:title="Save"
|
||||
app:showAsAction="always"/>
|
||||
<item
|
||||
android:id="@+id/menu_share"
|
||||
android:icon="@android:drawable/ic_menu_share"
|
||||
|
|
|
@ -1,4 +1,9 @@
|
|||
<resources>
|
||||
<string name="app_name">Simple Draw</string>
|
||||
<string name="share_via">Share via</string>
|
||||
<string name="saving_error">Could not save the file</string>
|
||||
<string name="saving_ok">Image has been saved successfully</string>
|
||||
<string name="file_name">File name</string>
|
||||
<string name="save_file">Save file</string>
|
||||
<string name="enter_file_name">Please enter a file name</string>
|
||||
</resources>
|
||||
|
|
Loading…
Reference in New Issue