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

相关推荐
simplepeng35 分钟前
我的天,我真是和androidx的字体加载杠上了
android
小猫猫猫◍˃ᵕ˂◍2 小时前
备忘录模式:快速恢复原始数据
android·java·备忘录模式
CYRUS_STUDIO4 小时前
使用 AndroidNativeEmu 调用 JNI 函数
android·逆向·汇编语言
梦否4 小时前
【Android】类加载器&热修复-随记
android
徒步青云4 小时前
Java内存模型
android
今阳4 小时前
鸿蒙开发笔记-6-装饰器之@Require装饰器,@Reusable装饰器
android·app·harmonyos
-优势在我9 小时前
Android TabLayout 实现随意控制item之间的间距
android·java·ui
hedalei9 小时前
android13修改系统Launcher不跟随重力感应旋转
android·launcher
Indoraptor11 小时前
Android Fence 同步框架
android
峥嵘life11 小时前
DeepSeek本地搭建 和 Android
android