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;
}
}
Android 调用系统分享,分享到Facebook
最新推荐文章于 2024-03-26 14:44:22 发布