安卓版本过高、权限收紧,通过Uril.fromFile
获取的Uri
无法分享,需要通过FileProvider
,适配如下:
代码调整
fun Context.shareFile(file: File) {
val intent = Intent()
.setAction(Intent.ACTION_SEND)
.setType("*/*")
.putExtra(Intent.EXTRA_STREAM, FileProvider.getUriForFile(this,"file_provider",file))
startActivity(Intent.createChooser(intent, getString(R.string.share_to)))
}
AndroidManifest.xml
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="file_provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/filepaths"/>
</provider>
filepaths 如下:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path
name="int_root"
path="/" />
<cache-path
name="app_cache"
path="/" />
<external-path
name="exe_root"
path="Documents/" />
<external-files-path
name="ext_pub"
path="/" />
<external-cache-path
name="ext_cache"
path="/" />
</paths>
注意:本文归作者所有,未经作者允许,不得转载