Android 调用系统分享,分享到Facebook

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;

import androidx.core.content.FileProvider;

import com.example.testany.App;

import java.io.File;

//调用系统分享
public class LocalShareUtil {

    /**
     * 分享文本内容
     * facebook分享要求只能是图片或者视频
     *
     * @param activity
     * @param text 分享文本
     * @param packageName 分享到指定App的包名,没有则展示所有系统分享
     */
    public static void shareText(Activity activity, String text, String packageName) {
        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (packageName != null)
            shareIntent.setPackage(packageName);
        shareIntent.setType("text/plain");//设置分享的格式
        shareIntent.putExtra(Intent.EXTRA_TEXT, text);
        //需要使用Intent.createChooser,否则会出现别样的应用选择框,您可以试试
        shareIntent = Intent.createChooser(shareIntent, "Share To");
        try {
            activity.startActivity(shareIntent);
        } catch (Exception e) {
            e.printStackTrace();
            L.e("sysShareText Exception:" + e.getMessage());
        }
    }

    /**
     * 调用系统分享
     *
     * @param activity
     * @param imgPath     分享图片路径
     * @param packageName 分享到指定App的包名,没有则展示所有系统分享
     */
    public static boolean shareImg(Activity activity, String imgPath, String packageName) {
        L.d("sysShareImg:" + imgPath);
        if (imgPath == null || activity == null) return false;
        Intent shareIntent = new Intent();
        //将mipmap中图片转换成Uri
        File file = new File(imgPath);
        if (!file.exists()) return false;
        Uri imgUri = null;
        // 判断版本大于等于7.0
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            // "项目包名.fileprovider"即是在清单文件中配置的authorities
            imgUri = FileProvider.getUriForFile(App.getContext(), (activity.getPackageName() + ".fileprovider"), file);
            // 给目标应用一个临时授权
        } else {
            imgUri = Uri.fromFile(file);
        }
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        shareIntent.setType("image/*");
        shareIntent.putExtra(Intent.EXTRA_STREAM, imgUri);
        shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        if (packageName != null)
            shareIntent.setPackage(packageName);
        //切记需要使用Intent.createChooser,否则会出现别样的应用选择框
        shareIntent = Intent.createChooser(shareIntent, "Share To");
        try {
            activity.startActivity(shareIntent);
        } catch (Exception e) {
            e.printStackTrace();
            L.e("sysShareImg Exception:" + e.getMessage());
            return false;
        }
        return true;
    }

    /**
     * 调用系统分享到facebook【只显示文字 分享图片调用sysShareImg】
     *
     * @param activity
     * @param text     分享文字+url  最终只显示链接内容
     */
    public static boolean shareToFacebook(Activity activity, String text) {
        L.d("sysShareToFacebook:" + text);
        if (text == null || activity == null) return false;
        Intent shareIntent = new Intent();
        File file = new File(Environment.getExternalStorageDirectory() + "/app/a.jpg");
        if (!file.exists()) return false;
        Uri imgUri = null;
        // 判断版本大于等于7.0
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            // "项目包名.fileprovider"即是在清单文件中配置的authorities
            imgUri = FileProvider.getUriForFile(App.getContext(), (activity.getPackageName() + ".fileprovider"), file);
            // 给目标应用一个临时授权
        } else {
            imgUri = Uri.fromFile(file);
        }
        // 给目标应用一个临时授权
        shareIntent.setPackage("com.facebook.katana");
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        shareIntent.putExtra(Intent.EXTRA_STREAM, imgUri);
//        shareIntent.setType("image/*");
//        shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        shareIntent.setType("text/plain");//设置分享的格式
        shareIntent.putExtra(Intent.EXTRA_TEXT, text);
//        shareIntent.putExtra("Kdescription", text);
        //切记需要使用Intent.createChooser,否则会出现别样的应用选择框
        shareIntent = Intent.createChooser(shareIntent, "Share To");
        try {
            activity.startActivity(shareIntent);
        } catch (Exception e) {
            e.printStackTrace();
            L.e("sysShareImg Exception:" + e.getMessage());
            return false;
        }
        return true;
    }
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值