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 一致。

相关推荐
2401_865721331 天前
御网杯初赛2026
android
葱段1 天前
【Compose】输入框(TextField)点击空白处失焦并关闭软键盘
android
刮风那天1 天前
Android Framework 核心架构图
android
__Witheart__1 天前
3588 安卓编译空间不足报错
android
aaajj1 天前
【Android】手机屏幕劫持防护
android·智能手机
写做四月一日的四月一日1 天前
在安卓手机上安装小龙虾openclaw并配置QQ机器人接入
android·人工智能
流星白龙1 天前
【MySQL高阶】6.MySQL数据目录,日志
android·mysql·adb
福大大架构师每日一题1 天前
rust 1.96.0 更新:语言、编译器、Cargo、Rustdoc、兼容性全面升级,必看完整解读
android·开发语言·rust
城管不管1 天前
Agent——001
android·java·数据库·llm·prompt
刮风那天1 天前
Android 理解onTransitionReady(一)
android