From 6d5d74a1587574e15406b91d510666632a0b0d68 Mon Sep 17 00:00:00 2001 From: tibbi Date: Fri, 26 Feb 2016 23:51:42 +0100 Subject: [PATCH] implement the sharing functionality --- app/src/main/AndroidManifest.xml | 10 ++++ .../simplemobiletools/draw/MainActivity.java | 54 +++++++++++++++++++ .../com/simplemobiletools/draw/MyCanvas.java | 9 ++++ app/src/main/res/values/strings.xml | 1 + app/src/main/res/xml/filepaths.xml | 6 +++ 5 files changed, 80 insertions(+) create mode 100644 app/src/main/res/xml/filepaths.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 601f3da..47305ce 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -18,5 +18,15 @@ + + + + diff --git a/app/src/main/java/com/simplemobiletools/draw/MainActivity.java b/app/src/main/java/com/simplemobiletools/draw/MainActivity.java index 92972d8..cb6fb97 100644 --- a/app/src/main/java/com/simplemobiletools/draw/MainActivity.java +++ b/app/src/main/java/com/simplemobiletools/draw/MainActivity.java @@ -1,18 +1,31 @@ package com.simplemobiletools.draw; +import android.content.Intent; +import android.graphics.Bitmap; import android.graphics.Color; +import android.net.Uri; import android.os.Bundle; +import android.support.v4.content.FileProvider; import android.support.v7.app.AppCompatActivity; +import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; + import butterknife.Bind; import butterknife.ButterKnife; import butterknife.OnClick; import yuku.ambilwarna.AmbilWarnaDialog; 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"; @Bind(R.id.my_canvas) MyCanvas myCanvas; @Bind(R.id.color_picker) View colorPicker; private int color; @@ -44,7 +57,48 @@ public class MainActivity extends AppCompatActivity { } private void shareImage() { + final String shareTitle = getResources().getString(R.string.share_via); + final Bitmap bitmap = myCanvas.getBitmap(); + final Intent sendIntent = new Intent(); + final Uri uri = getImageUri(bitmap); + if (uri == null) + return; + sendIntent.setAction(Intent.ACTION_SEND); + sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); + sendIntent.setDataAndType(uri, getContentResolver().getType(uri)); + sendIntent.putExtra(Intent.EXTRA_STREAM, uri); + sendIntent.setType("image/*"); + startActivity(Intent.createChooser(sendIntent, shareTitle)); + } + + private Uri getImageUri(Bitmap bitmap) { + final ByteArrayOutputStream bytes = new ByteArrayOutputStream(); + bitmap.compress(Bitmap.CompressFormat.PNG, 0, bytes); + + final File folder = new File(getCacheDir(), FOLDER_NAME); + if (!folder.exists()) { + if (!folder.mkdir()) + return null; + } + + final File file = new File(folder, FILE_NAME); + FileOutputStream fileOutputStream = null; + try { + fileOutputStream = new FileOutputStream(file); + fileOutputStream.write(bytes.toByteArray()); + } catch (Exception e) { + Log.e(TAG, "getImageUri 1 " + e.getMessage()); + } finally { + try { + if (fileOutputStream != null) + fileOutputStream.close(); + } catch (IOException e) { + Log.e(TAG, "getImageUri 2 " + e.getMessage()); + } + } + + return FileProvider.getUriForFile(this, "com.simplemobiletools.draw.fileprovider", file); } @OnClick(R.id.undo) diff --git a/app/src/main/java/com/simplemobiletools/draw/MyCanvas.java b/app/src/main/java/com/simplemobiletools/draw/MyCanvas.java index 1fe3320..5567705 100644 --- a/app/src/main/java/com/simplemobiletools/draw/MyCanvas.java +++ b/app/src/main/java/com/simplemobiletools/draw/MyCanvas.java @@ -1,6 +1,7 @@ package com.simplemobiletools.draw; import android.content.Context; +import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; @@ -55,6 +56,14 @@ public class MyCanvas extends View { color = newColor; } + public Bitmap getBitmap() { + final Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888); + final Canvas canvas = new Canvas(bitmap); + canvas.drawColor(Color.WHITE); + draw(canvas); + return bitmap; + } + @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index eb51514..4ff1031 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,3 +1,4 @@ Simple Draw + Share via diff --git a/app/src/main/res/xml/filepaths.xml b/app/src/main/res/xml/filepaths.xml new file mode 100644 index 0000000..b414b7d --- /dev/null +++ b/app/src/main/res/xml/filepaths.xml @@ -0,0 +1,6 @@ + + + +