From f06aad33cd41a40bf38d9007f27b6eb5fe997974 Mon Sep 17 00:00:00 2001 From: "Roman A. Sarria" Date: Sat, 10 Oct 2015 00:46:21 -0300 Subject: [PATCH] Fix for sharing --- README.md | 4 ++- package.json | 2 +- plugin.xml | 3 +- src/android/PhotoActivity.java | 63 +++++++++++++++++++++++++++++++--- 4 files changed, 65 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 6b3734d..efdcf10 100644 --- a/README.md +++ b/README.md @@ -33,4 +33,6 @@ PhotoViewer.show('http://my_site.com/my_image.jpg', 'Optional Title'); - Automatic close on error. - Support for content:// Uris from Cordova - Replaced old namespace -- Published to NPM +- Published to NPM +(1.1.1) +- Fix for sharing due to online restriction diff --git a/package.json b/package.json index 4fcfbfd..695997c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "com-sarriaroman-photoviewer", - "version": "1.0.2", + "version": "1.1.1", "description": "This plugin is intended to show a picture from an URL into a Photo Viewer with zoom features.", "cordova": { "id": "com-sarriaroman-photoviewer", diff --git a/plugin.xml b/plugin.xml index e03cc2b..c2fc3b7 100644 --- a/plugin.xml +++ b/plugin.xml @@ -1,5 +1,5 @@ - + PhotoViewer This plugin is intended to show a picture from an URL into a Photo Viewer with zoom features. @@ -20,6 +20,7 @@ + diff --git a/src/android/PhotoActivity.java b/src/android/PhotoActivity.java index 2e7789c..40da16d 100644 --- a/src/android/PhotoActivity.java +++ b/src/android/PhotoActivity.java @@ -2,7 +2,11 @@ import uk.co.senab.photoview.PhotoViewAttacher; import android.app.Activity; +import android.graphics.Bitmap; +import android.graphics.drawable.BitmapDrawable; +import android.graphics.drawable.Drawable; import android.os.Bundle; +import android.os.Environment; import android.view.View; import android.widget.ImageButton; import android.widget.ImageView; @@ -14,6 +18,10 @@ import com.squareup.picasso.Picasso; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; + public class PhotoActivity extends Activity { private PhotoViewAttacher mAttacher; @@ -34,6 +42,9 @@ protected void onCreate(Bundle savedInstanceState) { // Load the Views findViews(); + // Hide the Share Button + shareBtn.setVisibility(View.INVISIBLE); + // Change the Activity Title String actTitle = this.getIntent().getStringExtra("title"); if( !actTitle.equals("") ) { @@ -53,12 +64,16 @@ public void onClick(View v) { shareBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - Intent sharingIntent = new Intent(Intent.ACTION_SEND); + Uri bmpUri = getLocalBitmapUri(photo); + + if (bmpUri != null) { + Intent sharingIntent = new Intent(Intent.ACTION_SEND); - sharingIntent.setType("image/*"); - sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(imageUrl)); + sharingIntent.setType("image/*"); + sharingIntent.putExtra(Intent.EXTRA_STREAM, bmpUri); - startActivity(Intent.createChooser(sharingIntent, "Share")); + startActivity(Intent.createChooser(sharingIntent, "Share")); + } } }); @@ -96,6 +111,8 @@ private Activity getActivity() { */ private void hideLoadingAndUpdate() { photo.setVisibility(View.VISIBLE); + shareBtn.setVisibility(View.VISIBLE); + mAttacher.update(); } @@ -127,4 +144,42 @@ public void onError() { } } + /** + * Create Local Image due to Restrictions + * + * @param imageView + * + * @return + */ + public Uri getLocalBitmapUri(ImageView imageView) { + Drawable drawable = imageView.getDrawable(); + Bitmap bmp = null; + + if (drawable instanceof BitmapDrawable){ + bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap(); + } else { + return null; + } + + // Store image to default external storage directory + Uri bmpUri = null; + try { + File file = new File( + Environment.getExternalStoragePublicDirectory( + Environment.DIRECTORY_DOWNLOADS + ), "share_image_" + System.currentTimeMillis() + ".png"); + + file.getParentFile().mkdirs(); + + FileOutputStream out = new FileOutputStream(file); + bmp.compress(Bitmap.CompressFormat.PNG, 90, out); + out.close(); + + bmpUri = Uri.fromFile(file); + } catch (IOException e) { + e.printStackTrace(); + } + return bmpUri; + } + }