Skip to content

Commit

Permalink
added image sharing on android
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Narolewski committed Feb 9, 2016
1 parent 0822107 commit a5d24c8
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 7 deletions.
55 changes: 50 additions & 5 deletions extension/share/Share.hx
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package extension.share;

//#if blackberry
import sys.io.File;
import openfl.utils.ByteArray;
import openfl.display.JPEGEncoderOptions;
import sys.FileSystem;
import openfl.display.BitmapData;
typedef ShareQueryResult = {
key : String,
icon : String,
Expand All @@ -11,7 +16,7 @@ typedef ShareQueryResult = {
class Share {

#if android
private static var __share : String->String->String->String->Void=openfl.utils.JNI.createStaticMethod("shareex/ShareEx", "share", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
private static var __share : String->String->String->String->String->Void=openfl.utils.JNI.createStaticMethod("shareex/ShareEx", "share", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
#elseif ios
private static var __share : String->String->String->Void=cpp.Lib.load("openflShareExtension","share_do",3);
#elseif blackberry
Expand All @@ -25,6 +30,7 @@ class Share {
public static var defaultFallback:String->Void=null;
public static var facebookRedirectURI:String=null;
public static var defaultSubject:String='';
private static var sharedImagePath:String = "";

public static inline var FACEBOOK:String='facebook';
public static inline var TWITTER:String='twitter';
Expand Down Expand Up @@ -71,16 +77,56 @@ class Share {
#end

}

public static function share(text:String, subject:String=null, image:String='', html:String='', email:String='', url:String=null, socialNetwork:String=null, fallback:String->Void=null){
private static function prepareBitmapData(bdm:BitmapData):Void
{
var imagePath:String = "";
sharedImagePath = "";
#if !lime_legacy
imagePath = lime.system.System.documentsDirectory + "/shareimage.jpg";
#else
imagePath = openfl.utils.SystemPath.documentsDirectory + "/shareimage.jpg";
#end
if (FileSystem.exists(imagePath))
{
try
{
FileSystem.deleteFile(imagePath);
}
catch(e:Dynamic)
{
trace("deleting image failed");
return;
}
}
var bytes:ByteArray = bdm.encode(bdm.rect, new JPEGEncoderOptions());
try
{
File.saveBytes(imagePath, bytes);
}
catch(e:Dynamic)
{
trace("saving image failed");
return;
}
sharedImagePath = imagePath;
}
public static function share(text:String, subject:String=null, image:String='', html:String='', email:String='', url:String=null, socialNetwork:String=null, fallback:String->Void=null, bdm:BitmapData = null){
if(url==null) url=defaultURL;
if(subject==null) subject=defaultSubject;
if(socialNetwork==null) socialNetwork=defaultSocialNetwork;
if(fallback==null) fallback=defaultFallback;
var cleanUrl:String=StringTools.replace(StringTools.replace(url,'http://',''),'https://','');
try{

#if (android || ios)

if(bdm != null)
prepareBitmapData(bdm);
else
sharedImagePath = "";
#end
#if android
__share(text+(cleanUrl!='' ? ' '+cleanUrl : ''),subject,html,email);
__share(text+(cleanUrl!='' ? ' '+cleanUrl : ''),subject,html,email,sharedImagePath);
#elseif ios
__share(text,url==''?null:url,subject==''?null:subject);
#elseif blackberry
Expand Down Expand Up @@ -115,5 +161,4 @@ class Share {
trace("Share SHARE Exception: "+e);
}
}

}
2 changes: 2 additions & 0 deletions java/ShareEx/include.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<project>
<java path="project/android" if="android" />
<android permission="android.permission.WRITE_EXTERNAL_STORAGE" />
<android permission="android.permission.READ_EXTERNAL_STORAGE" />
</project>
13 changes: 11 additions & 2 deletions java/ShareEx/project/android/shareex/ShareEx.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
package shareex;

import android.content.Intent;
import android.net.Uri;
import org.haxe.extension.Extension;

public class ShareEx {

public static void share(String text, String subject, String html, String email) {
public static void share(String text, String subject, String html, String email, String image) {
Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND);
sendIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
sendIntent.setType("text/plain");
if(image == "")
sendIntent.setType("text/plain");
else
{
sendIntent.setType("image/*");
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + image));
sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
}

if(subject!=null && subject!="") sendIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
if(text!=null && text!="") sendIntent.putExtra(android.content.Intent.EXTRA_TEXT, text);
if(html!=null && html!="") sendIntent.putExtra(android.content.Intent.EXTRA_HTML_TEXT, html);
Expand Down

0 comments on commit a5d24c8

Please sign in to comment.