Android 7.0之前,文件的Uri以
file:///形式
提供给其他app访问。Android 7.0之后,为了安全起见,
file:///形式
的Uri不能正常访问,官方提 供了FileProvider,FileProvider生成的Uri会以content://的形式
分享给其他app使用。
那如何使用FileProvider?
/"/>
最终生成的代码效果
以第二个为例:
content://com.flx.cn.fileprovider/my_files/filexxx.jpg
File filePath = new File(Context.getFilesDir(), "my_log");File newFile = new File(filePath, "my_log.log");// 生成Uri
Uri contentUri = FileProvider.getUriForFile(getContext(), "com.flx.cn.fileprovider", newFile);
// 这里用的是发送文件。
Intent intent = new Intent(Intent.ACTION_SEND);// 设置读写权限
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);//Uri传入Intent
intent.putExtra(Intent.EXTRA_STREAM, contentUri);startActivity(intent)