我正在尝试从相机获取图像并将其直接保存到我的应用程序的私人文件目录。出于安全考虑,图像不应在任何时候公开访问。
通常,授予对私有文件的临时访问权的方式是使用contentprovider并在意图中设置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/文件路径.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);
但是,这会导致camera应用程序立即返回而不做任何操作。我怀疑是因为它不希望有任何有意的数据集(intent.set data())。
上面的策略效果不太好。那么,如何安全地将从相机捕获的图像直接保存到应用程序的私人文件目录?
最佳答案
那么,如何安全地将从相机捕获的图像直接保存到应用程序的私人文件目录?
使用android.hardware.Camera
亲自拍照。不能保证用户会有一个摄像头应用程序知道如何将数据保存到content://
Uri
路径。他们应该支持他们,但不是所有人都支持。例如,截至2016年6月,google camera应用程序不支持content://
Uri
forACTION_VIDEO_CAPTURE
。