implement the sharing functionality
This commit is contained in:
parent
bb16867d8e
commit
6d5d74a158
|
@ -18,5 +18,15 @@
|
|||
<category android:name="android.intent.category.LAUNCHER"/>
|
||||
</intent-filter>
|
||||
</activity>
|
||||
|
||||
<provider
|
||||
android:name="android.support.v4.content.FileProvider"
|
||||
android:authorities="com.simplemobiletools.draw.fileprovider"
|
||||
android:exported="false"
|
||||
android:grantUriPermissions="true">
|
||||
<meta-data
|
||||
android:name="android.support.FILE_PROVIDER_PATHS"
|
||||
android:resource="@xml/filepaths"/>
|
||||
</provider>
|
||||
</application>
|
||||
</manifest>
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
<resources>
|
||||
<string name="app_name">Simple Draw</string>
|
||||
<string name="share_via">Share via</string>
|
||||
</resources>
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<paths>
|
||||
<cache-path
|
||||
name="shared_images"
|
||||
path="images/"/>
|
||||
</paths>
|
Loading…
Reference in New Issue