Skip to content

Commit

Permalink
Fix for sharing
Browse files Browse the repository at this point in the history
  • Loading branch information
sarriaroman committed Oct 10, 2015
1 parent b607660 commit f06aad3
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 7 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
3 changes: 2 additions & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version='1.0' encoding='utf-8' ?>
<plugin id="com.sarriaroman.PhotoViewer" version="1.1.0" xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android">
<plugin id="com.sarriaroman.PhotoViewer" version="1.1.1" xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android">
<name>PhotoViewer</name>
<description>This plugin is intended to show a picture from an URL into a Photo Viewer with zoom features.</description>
<js-module name="PhotoViewer" src="www/PhotoViewer.js">
Expand All @@ -20,6 +20,7 @@
<config-file parent="/manifest" target="AndroidManifest.xml">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
</config-file>

<source-file src="src/android/PhotoViewer.java" target-dir="src/com/sarriaroman/PhotoViewer" />
Expand Down
63 changes: 59 additions & 4 deletions src/android/PhotoActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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("") ) {
Expand All @@ -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"));
}
}
});

Expand Down Expand Up @@ -96,6 +111,8 @@ private Activity getActivity() {
*/
private void hideLoadingAndUpdate() {
photo.setVisibility(View.VISIBLE);
shareBtn.setVisibility(View.VISIBLE);

mAttacher.update();
}

Expand Down Expand Up @@ -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;
}

}

0 comments on commit f06aad3

Please # to comment.