android 分享文件

1.在AndroidManifest.xml 中配置 FileProvider

复制代码
<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="com.example.caliv.ffyy.fileProvider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths" />
</provider>
复制代码
2.在file_paths.xml中配置路劲

3.创建方法 传如参数

复制代码
public static void sendFile(Context context,File file){
    if (null != file && file.exists()) {
        Intent share = new Intent(Intent.ACTION_SEND);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Uri contentUri = FileProvider.getUriForFile(context, "com.example.caliv.ffyy.fileProvider",file);
            share.putExtra(Intent.EXTRA_STREAM, contentUri);
            share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        } else {
            share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
        }
        share.setType("*/*");//此处可发送多种文件
        share.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        context.startActivity(Intent.createChooser(share, "分享文件"));
    } else {
        Toast.makeText(context, "分享文件不存在", Toast.LENGTH_SHORT).show();
    }
}

红色代码处的代码应与AndroidManifest.xml中的FileProvider 一致。

相关推荐
雨白3 小时前
掌握 NestedScrolling 嵌套滑动:手写仿知乎折叠主页
android
Xzaveir5 小时前
别把所有“认证”都塞进 AuthService:实名、一键登录与号码身份的领域拆分
android·人工智能
BerrySen1785 小时前
KMP全栈开发:从Android到AI Agent的技术演进与实践
android·人工智能
AFinalStone6 小时前
Android 7系统休眠唤醒(一)电源管理架构全景图
android·powermanager·电源管理
YM52e6 小时前
鸿蒙Flutter Center居中组件:Align对齐详解
android·学习·flutter·华为·harmonyos·鸿蒙
时间的拾荒人7 小时前
MySQL 视图详解
android·数据库·mysql
祉猷并茂,雯华若锦8 小时前
Appium 3.x安卓按键与通知栏操作全指南
android·appium
码农coding9 小时前
android 12 SurfaceFlinger开机启动分析
android
hunterandroid9 小时前
Paging 3 RemoteMediator 实战:构建离线优先的分页列表
android·前端
码农coding9 小时前
android12 开机启动PMS
android