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. dagger.android 源码
  2. 《Android系统学习》之JAVA与C混合编程——JNI
  3. 获取mic音量大小
  4. Android7.0系统应用包名信息
  5. Android移植之系统配置新产品篇
  6. Android开发名词解释
  7. 获取系统版本内核版本信息
  8. googlemap学习
  9. Android(安卓)A/B System概述

随机推荐

  1. Android横屏竖屏切换的问题
  2. Android(安卓)3.0 r1中文API文档(104) ―
  3. Android(安卓)CTS(兼容性测试)
  4. 开发环境搭建
  5. android:inputType标签
  6. 自定义tabhost实现
  7. Android中android:visibility的3中属性的
  8. Android知识点
  9. 【Android】Android上的Jetty
  10. 如何打包你自己的apk程序到Android里?