public static boolean sendFileToOtherApp(Activity context, File file, AppType type)
{
final Uri uri;
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= 24) {//若SDK大于等于24 获取uri采用共享文件模式
uri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileProvider", file);
} else {
uri = Uri.fromFile(file);
}
Intent share = new Intent(Intent.ACTION_SEND);
share.putExtra(Intent.EXTRA_STREAM, uri);
String extension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(file).toString());
String mimetype = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
share.setType(mimetype);//此处可发送多种文件
share.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //注释掉,会出现发完文件当前app在多任务界面不显示,疑似新旧任务栈一起了,放到Intent.createChooser生成的intent中,分享完返回回到自己app
share.addCategory(Intent.CATEGORY_DEFAULT);
share.setPackage(type.toString());
if (share.resolveActivity(context.getPackageManager()) != null)
{
context.startActivity(share);
return true;
} else {
return false;
}
}
传文件选始终后卸载重装发文件还是不会跳选择框
//每次开启选择器选择打开方式
Intent.createChooser(intent, "分享")
网友评论