android.os.FileUriExposedException:<filename>通过Intent.getData()暴露给应用程序之外。
创始人
2024-08-20 02:30:08
0

Android 7.0及更高版本中,引入了FileUriExposedException异常,用于防止应用程序使用file:// URI暴露文件给其他应用。为了解决这个问题,可以使用FileProvider来生成content:// URI,以便在应用之间共享文件。

以下是一个解决方法的示例代码:

首先,在AndroidManifest.xml文件中添加FileProvider的声明:


    
        ...
        
            
        
    

然后,在res/xml目录下创建file_paths.xml文件,指定要共享的文件路径:



    

接下来,在需要共享文件的地方,使用FileProvider生成content:// URI,并将其传递给Intent:

// 获取文件的路径
File file = new File(filePath);
// 通过FileProvider生成content:// URI
Uri fileUri = FileProvider.getUriForFile(context, "com.example.myapp.fileprovider", file);

// 将content:// URI传递给Intent
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/jpeg");
intent.putExtra(Intent.EXTRA_STREAM, fileUri);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);

注意:在使用Intent传递content:// URI时,需要为Intent添加FLAG_GRANT_READ_URI_PERMISSION标志,以授予接收方应用程序对该URI的读取权限。

这样就可以避免android.os.FileUriExposedException异常,并安全地共享文件给其他应用程序。

相关内容

热门资讯

透视工具!aapoker安装包... 透视工具!aapoker安装包怎么使用(透视)aapoker万能辅助器,AA德州教程(有挂揭秘)1、...
透视数据!aapoker辅助工... 透视数据!aapoker辅助工具安全吗(透视)aapoker插件,安装教程(有挂细节);1、这是跨平...
透视总结!aapoker怎么设... 透视总结!aapoker怎么设置提高好牌几率(透视)aapoker辅助器是真的吗,解密教程(有挂揭秘...
透视系统!aapoker如何设... 透视系统!aapoker如何设置胜率(透视)aa poker透视软件,2025新版教程(有挂解说)1...
透视挂!aapoker辅助器怎... 透视挂!aapoker辅助器怎么用(透视)aapoker ai插件,解密教程(有挂揭秘)1.aapo...
透视中牌率!aa poker透... 透视中牌率!aa poker透视软件(透视)aapoker透视插件,解密教程(有挂解说)1、操作简单...
透视规律!aapoker免费透... 透视规律!aapoker免费透视脚本(透视)aapoker插件下载,AI教程(有挂揭秘)1、aapo...
透视新版!aapoker辅助工... 透视新版!aapoker辅助工具安全吗(透视)aapoker万能辅助器,教你攻略(有挂黑科技)1、构...
透视系统!aapoker安装包... 透视系统!aapoker安装包怎么使用(透视)aapoker透视插件,解说技巧(有挂细节)小薇(透视...
透视透视!aapoker俱乐部... 透视透视!aapoker俱乐部靠谱吗(透视)aapoker俱乐部靠谱吗,黑科技教程(有挂细节);1、...