fix:修复 android 无法分享文件的问题

This commit is contained in:
anfe 2024-06-24 14:28:15 +08:00
parent 5b09b91c84
commit 3ae670fbee
3 changed files with 12 additions and 7 deletions

View File

@ -62,9 +62,10 @@
android:label="@string/app_name"
android:requestLegacyExternalStorage="true"
android:usesCleartextTraffic="true">
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data

View File

@ -68,21 +68,22 @@ class MainActivity : FlutterActivity() {
if (filePath == null) {
return
}
val file = File(filePath)
if (!file.exists()) {
Log.e("File Share", "文件不存在: $filePath")
return
}
val uri: Uri = FileProvider.getUriForFile(
this,
"${applicationContext.packageName}.fileprovider",
"${packageName}.provider",
file
)
val shareIntent = Intent().apply {
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_STREAM, uri)
type = "application/octet-stream"
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
startActivity(Intent.createChooser(shareIntent, null))
}

View File

@ -1,4 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="files" path="." />
<paths>
<root-path name="root-path" path="."/>
<files-path name="files" path="."/>
<cache-path name="cache" path="."/>
<external-files-path name="external_files" path="."/>
</paths>