美文网首页
App开启QQ微信传文件

App开启QQ微信传文件

作者: 霁逸lei | 来源:发表于2022-06-16 21:39 被阅读0次
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;
        }
    }

Android利用系统系统自带文件分享功能

传文件选始终后卸载重装发文件还是不会跳选择框

//每次开启选择器选择打开方式
Intent.createChooser(intent, "分享")

相关文章

网友评论

      本文标题:App开启QQ微信传文件

      本文链接:https://www.haomeiwen.com/subject/intvprtx.html