Android:如何将相机结果保存到私人文件

前端之家收集整理的这篇文章主要介绍了Android:如何将相机结果保存到私人文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图从相机中获取图像并将其直接保存到我的应用程序的私人文件目录中.出于安全考虑,图像不应随时公开访问.

通常,授予对私有文件的临时访问权限的方式是使用ContentProvider并在Intent中设置GRANT_WRITE_URI_PERMISSION标志.按照FileProvider中的文档,我完成了以下操作:

AndroidManfiest.xml

@H_301_6@<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

@H_301_6@<paths xmlns:android="http://schemas.android.com/apk/res/android"> <files-path name="my_images" path="images/"/> </paths>

启动相机活动时,我从活动中执行以下操作:

@H_301_6@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上有任何数据集(Intent.setData()).

上述策略效果不佳.那么,如何将从相机捕获的图像直接安全地保存到我应用程序的私人文件目录中?

解决方法

So,how can I securely save an image captured from the camera directly to my app’s private files directory?

使用android.hardware.Camera自己拍照.无法保证用户将拥有可用的相机应用程序知道如何将数据保存到内容:// Uri路径.他们应该支持他们,但不是全部都支持他们.例如,Google相机应用不支持内容:// Uri for ACTION_VIDEO_CAPTURE截至2016年6月.

原文链接:https://www.f2er.com/android/315193.html

猜你在找的Android相关文章