【发布时间】:2018-01-26 19:45:38
【问题描述】:
随着 android 正在提高其安全性并改变一些东西以使 Android 操作系统更安全地抵御漏洞,了解新结构让开发人员进入它是一件好事。 过去几年我一直远离开发。我只是注意到一些东西已经改变,例如 URI 的文件方案。
案例 1:: 我正在使用下面提到的自定义 URI 启动相机意图。
var takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(packageManager) != null) {
// Create the File where the photo should go
var photoFile: File? = null
try {
photoFile = createImageFile()
} catch (ex: IOException) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
val photoURI: Uri = FileProvider.getUriForFile(this,
BuildConfig.APPLICATION_ID + ".fileprovider",
photoFile)
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI)
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO)
}
}
photoURI 是自定义 URI。 当我拍照并检查 URI 时,它有 file:// 开头,我可以从那里获取文件路径,我可以用它做一些事情(我正在将它上传到服务器)
案例 2
我正在启动从图库中挑选图片的简单意图
val intent = Intent()
intent.type = "image/*"
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
intent.action = Intent.ACTION_GET_CONTENT
startActivityForResult(Intent.createChooser(intent, "Select Picture"), TYPE_IMAGE)
现在,当我选择图像并检查 URI 时,它的开头有 content://。当我尝试从中获取文件路径时,麻烦就开始了。我一直在搜索 SOF,但没有一个解决方案适合我。
我获取文件路径的主要目的是我想从设备中选择文件并将其上传到服务器。但是使用 content:// 方案我无法从 URI 制作文件,因此无法发送此文件服务器。
我刚刚阅读了CommonsWareBlog 关于方案系统的信息。它清除了一些东西,但我仍然不知道如何在我的 onActivityResult 中从 content:// 和 file:// 切换!
我想再次提一下,目前关于 SOF 的答案对我不起作用,这就是为什么我必须详细询问这个问题。
我正在使用 7.0 设备模拟器
这只是我的问题的一个例子,实际上我正在拾取 PDF 和 DOCX 文件并希望将它们上传到服务器上,在拾取文件后,我有带有 content:// 方案的 URI,我无法制作文件并发送到服务器。
我不确定我在计划中缺少什么。 我希望这个问题能帮助许多其他不熟悉文件系统的开发人员。
请根据我从意图中选择文件的问题尝试通过代码示例进行解释(如果您愿意)
【问题讨论】:
标签: android android-intent android-contentprovider