【发布时间】:2013-11-18 21:56:25
【问题描述】:
我正在尝试从相机中获取图像并将其直接保存到我的应用程序的私有文件目录中。出于安全考虑,该图像不应在任何时候公开访问。
通常,您授予对私有文件的临时访问权限的方式是使用 ContentProvider 并在 Intent 中设置 GRANT_WRITE_URI_PERMISSION 标志。按照FileProvider 中的文档,我做了以下事情:
AndroidManfiest.xml
<manifest>
...
<application>
...
<provider
android:authorities="com.my.domain"
android:name="android.support.v4.content.FileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
...
res/xml/file_paths.xml
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="my_images" path="images/"/>
</paths>
当启动相机活动时,我从一个活动中执行以下操作:
File imageFile = getInternalImageFile();
Uri captureUri = FileProvider.getUriForFile(this, "com.my.domain", imageFile);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// grant temporary access to the file
intent.setData(captureUri);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
// tell the camera where to save the file
intent.putExtra(MediaStore.EXTRA_OUTPUT, captureUri);
startActivityForResult(intent, IMAGE_CAPTURE_REQUEST_CODE);
然而,这会导致相机应用程序立即返回而无需执行任何操作。我怀疑是因为它不希望有任何意图数据集 (Intent.setData())。
上述策略效果不佳。那么,如何安全地将摄像头拍摄的图像直接保存到应用的私有文件目录中呢?
【问题讨论】:
-
问题是授予对私有文件的访问权只有在应用程序实际使用它时才有效。在你的情况下,我会保存在任何地方,然后将其移动到私人存储中。
-
@njzk2 这实际上是我的应用程序目前所做的,但确定在任何时间段内将文件保存到外部存储(或任何可公开访问的内容)都存在安全风险。因此需要将其直接保存到内部存储中。
-
您可以随时在您的应用中重新实现相机活动。
标签: android android-camera-intent