我想共享我的Android应用程序中的文件.假设用户单击"共享…"按钮,它会打开一个对话框,让他们 Select 要与哪个应用程序共享.它可以是一个邮箱应用程序(在这种情况下,我希望它作为附件),或者是一个消息应用程序(消息作为附件发送给联系人),等等.
感觉文档中的Sharing a file个部分就是关于这一点的,但不知怎的,我不知道如何让它起作用.文档的重点是让另一个应用程序从我的应用程序中" Select "一个文件,我只想让我的应用程序与任意应用程序"共享"一个文件.
我试着将provider设置为AndroidManifest.xml
:
<provider
android:authorities="com.test.fileprovider"
android:name="androidx.core.content.FileProvider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
我创建了xml/filepaths
文件:
<paths>
<files-path path="logs/" name="logs" />
</paths>
然后我在下面的logFile
中写下我的文件:
val logDir = File(context.filesDir, "logs")
val logFile = File(logDir, "mylog.txt")
我try 与以下人员"分享":
val fileToShare = FileProvider.getUriForFile(
context,
"com.test.fileprovider",
logFile
)
val sendIntent = Intent().apply {
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_STREAM, fileToShare)
type = "*/*"
}
context.startActivity(sendIntent)
但是,尽管它没有崩溃,但什么也没有发生.
我错过了什么?这是共享文件的正确方式吗?