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

我试图从相机中获取图像并将其直接保存到我的应用程序的私人文件目录中.出于安全考虑,图像不应随时公开访问.

通常,授予对私有文件的临时访问权限的方式是使用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上有任何数据集(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月.

相关文章

这篇“android轻量级无侵入式管理数据库自动升级组件怎么实现...
今天小编给大家分享一下Android实现自定义圆形进度条的常用方...
这篇文章主要讲解了“Android如何解决字符对齐问题”,文中的...
这篇文章主要介绍“Android岛屿数量算法怎么使用”的相关知识...
本篇内容主要讲解“Android如何开发MQTT协议的模型及通信”,...
本文小编为大家详细介绍“Android数据压缩的方法是什么”,内...