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

相关推荐
六件套是我7 小时前
redission实现延时队列
android·java·servlet
00后程序员张12 小时前
iOS 上架费用全解析 开发者账号、App 审核、工具使用与开心上架(Appuploader)免 Mac 成本优化指南
android·macos·ios·小程序·uni-app·cocoa·iphone
来来走走12 小时前
Android开发(Kotlin) 扩展函数和运算符重载
android·开发语言·kotlin
wuwu_q12 小时前
用通俗易懂 + Android 开发实战的方式,详细讲解 Kotlin Flow 中的 retryWhen 操作符
android·开发语言·kotlin
天选之女wow12 小时前
【代码随想录算法训练营——Day60】图论——94.城市间货物运输I、95.城市间货物运输II、96.城市间货物运输III
android·算法·图论
沐怡旸13 小时前
【底层机制】Android对Linux线程调度的移动设备优化深度解析
android·面试
正经教主14 小时前
【咨询】Android Studio 第三方手机模拟器对比【202511】
android·ide·android studio
Jomurphys15 小时前
网络 - 缓存
android
似霰16 小时前
安卓14移植以太网&&framework-connectivity-t 编译问题
android·framework·安卓·ethernet