Skip to content

Commit

Permalink
0.4.0 Support sharing pictures to whatsapp
Browse files Browse the repository at this point in the history
  • Loading branch information
lizhuoyuan committed Jul 12, 2019
1 parent 114485d commit 323be41
Show file tree
Hide file tree
Showing 10 changed files with 289 additions and 106 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 0.4.0
Support sharing pictures to whatsapp

## 0.3.1
Allow developers to enter their own facebook id
Delete useless code
Expand Down
97 changes: 53 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ dependencies:
flutter:
sdk: flutter
# add flutter_ScreenUtil
flutter_share_me: ^0.3.1
flutter_share_me: ^0.4.0
```

## Usage
Expand All @@ -48,9 +48,9 @@ Add "facebook app id" to the application tag of AndroidManifest.xml

## Methods

#### shareToFacebook({String url, String msg})
#### shareToTwitter({String url, String msg})
#### shareToWhatsApp({String msg})
#### shareToFacebook({String msg, String url})
#### shareToTwitter({String msg, String url})
#### shareToWhatsApp({String base64ImageUrl,String msg})
#### shareToSystem({String msg}) use system share ui

These methods will return "success" if they successfully jump to the corresponding app.
Expand All @@ -63,46 +63,55 @@ These methods will return "success" if they successfully jump to the correspondi

## Example
```
Column(
children: <Widget>[
RaisedButton(
child: Text('share to twitter'),
onPressed: () async {
var response = await FlutterShareMe().shareToTwitter(
url: 'https://github.com/lizhuoyuan',
msg: 'hello flutter! ');
if (response == 'success') {
print('navigate success');
}
},
),
RaisedButton(
child: Text('share to shareWhatsApp'),
onPressed: () {
FlutterShareMe().shareToWhatsApp(
msg:
'hello,this is my github:https://github.com/lizhuoyuan');
},
),
RaisedButton(
child: Text('share to shareFacebook'),
onPressed: () {
FlutterShareMe().shareToFacebook(
url: 'https://github.com/lizhuoyuan', msg: 'Hello Flutter');
},
),
RaisedButton(
child: Text('share to System'),
onPressed: () async {
var response =
await FlutterShareMe().shareToSystem(msg: 'Hello Flutter');
if (response == 'success') {
print('navigate success');
}
},
),
],
),
Container(
width: double.infinity,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Image.memory(
base64.decode(base64Image.split(',')[1]),
height: 312,
width: 175.3,
fit: BoxFit.fill,
gaplessPlayback: true,
),
SizedBox(height: 30),
RaisedButton(
child: Text('share to twitter'),
onPressed: () async {
var response = await FlutterShareMe().shareToTwitter(
url: 'https://github.com/lizhuoyuan', msg: msg);
if (response == 'success') {
print('navigate success');
}
},
),
RaisedButton(
child: Text('share to shareWhatsApp'),
onPressed: () {
FlutterShareMe()
.shareToWhatsApp(base64ImageUrl: base64Image, msg: msg);
},
),
RaisedButton(
child: Text('share to shareFacebook'),
onPressed: () {
FlutterShareMe().shareToFacebook(
url: 'https://github.com/lizhuoyuan', msg: msg);
},
),
RaisedButton(
child: Text('share to System'),
onPressed: () async {
var response = await FlutterShareMe().shareToSystem(msg: msg);
if (response == 'success') {
print('navigate success');
}
},
),
],
),
)
```


2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ android {
}

dependencies {
implementation 'com.facebook.android:facebook-share:[4,5)'
implementation 'com.facebook.android:facebook-share:5.0.1'
implementation 'com.twitter.sdk.android:twitter:3.1.1' //twitter分享
}
9 changes: 9 additions & 0 deletions android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,14 @@
android:name="com.facebook.FacebookContentProvider"
android:authorities="com.facebook.app.FacebookContentProvider343254889799245"
android:exported="true" />
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;

import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.share.Sharer;
import com.facebook.share.model.ShareLinkContent;
import com.facebook.share.widget.ShareDialog;
Expand All @@ -22,6 +22,7 @@
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;
import zhuoyuan.li.fluttershareme.util.FileUtil;

/**
* FlutterShareMePlugin
Expand All @@ -30,35 +31,35 @@ public class FlutterShareMePlugin implements MethodCallHandler {

private Activity activity;
private static CallbackManager callbackManager;
private Registrar registrar;


/**
* Plugin registration.
*/
private FlutterShareMePlugin(MethodChannel channel, Activity activity) {
MethodChannel _channel = channel;
this.activity = activity;
_channel.setMethodCallHandler(this);
private FlutterShareMePlugin(Registrar registrar) {
this.activity = registrar.activity();
this.registrar = registrar;
}

/**
* Plugin registration.
*/
public static void registerWith(Registrar registrar) {
final MethodChannel channel = new MethodChannel(registrar.messenger(), "flutter_share_me");
channel.setMethodCallHandler(new FlutterShareMePlugin(channel, registrar.activity()));
channel.setMethodCallHandler(new FlutterShareMePlugin(registrar));
callbackManager = CallbackManager.Factory.create();
}

/**
* method
*
* @param call
* @param result
* @param call methodCall
* @param result Result
*/
@Override
public void onMethodCall(MethodCall call, Result result) {
String url, msg;

switch (call.method) {
case "shareFacebook":
url = call.argument("url");
Expand All @@ -72,7 +73,9 @@ public void onMethodCall(MethodCall call, Result result) {
break;
case "shareWhatsApp":
msg = call.argument("msg");
shareWhatsApp(msg, result);
url = call.argument("url");

shareWhatsApp(url, msg, result);
break;
case "system":
msg = call.argument("msg");
Expand All @@ -85,7 +88,7 @@ public void onMethodCall(MethodCall call, Result result) {
}

/**
* 调用系统分享
* system share
*
* @param msg String
* @param result Result
Expand All @@ -110,7 +113,6 @@ private void shareSystem(Result result, String msg) {
* @param result Result
*/
private void shareToTwitter(String url, String msg, Result result) {
//这里分享一个链接,更多分享配置参考官方介绍:https://dev.twitter.com/twitterkit/android/compose-tweets
try {
TweetComposer.Builder builder = new TweetComposer.Builder(activity)
.text(msg);
Expand Down Expand Up @@ -170,17 +172,28 @@ public void onError(FacebookException error) {
* @param msg String
* @param result Result
*/
private void shareWhatsApp(String msg, Result result) {
private void shareWhatsApp(String url, String msg, Result result) {
try {
Intent textIntent;
textIntent = new Intent("android.intent.action.SEND");
textIntent.setType("text/plain");
textIntent.setPackage("com.whatsapp");
textIntent.putExtra("android.intent.extra.TEXT", msg);
activity.startActivity(textIntent);
Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
whatsappIntent.setType("text/plain");
whatsappIntent.setPackage("com.whatsapp");
whatsappIntent.putExtra(Intent.EXTRA_TEXT, msg);

if (!TextUtils.isEmpty(url)) {
FileUtil fileHelper = new FileUtil(registrar, url);
if (fileHelper.isFile()) {
whatsappIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
whatsappIntent.putExtra(Intent.EXTRA_STREAM, fileHelper.getUri());
whatsappIntent.setType(fileHelper.getType());
}
}
whatsappIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
activity.startActivity(whatsappIntent);
result.success("success");
} catch (Exception var9) {
result.error("error", var9.toString(), "");
}
}


}
Loading

0 comments on commit 323be41

Please sign in to comment.