Android:如何将相机结果保存到私人文件 [英] Android: How to save the camera result to a private file

查看:160
本文介绍了Android:如何将相机结果保存到私人文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to get an image from the camera and save it directly to my app's private files directory. For security concerns, the image should not be publicly accessible at any time.

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

In general, the way you grant temporary access to a private file is to use a ContentProvider and set the GRANT_WRITE_URI_PERMISSION flag in the Intent. Following the documentation in FileProvider, I have done the following:

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>

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

When launching the camera activity, I execute the following from an activity:

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())上设置任何数据.

This however results in the camera app returning immediately without doing anything. I suspect because it's not expecting there to be any data set on intent (Intent.setData()).

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

The above strategy isn't working out so well. So, how can I securely save an image captured from the camera directly to my app's private files directory?

推荐答案

那么,如何安全地将从相机捕获的图像直接保存到应用程序的私有文件目录中?

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

使用android.hardware.Camera自己拍摄照片.不能保证用户将拥有一个相机应用程序,该应用程序知道如何将数据保存到content:// Uri路径.应该应该支持他们,但并非所有人都支持.例如,直到2016年6月,Google Camera应用都不支持ACTION_VIDEO_CAPTUREcontent:// Uri.

Take the picture yourself, using android.hardware.Camera. There is no guarantee that the user will have a camera app available that knows how to save data to content:// Uri paths. They are supposed to support them, but not all do. For example, the Google Camera app did not support a content:// Uri for ACTION_VIDEO_CAPTURE up through June 2016.

这篇关于Android:如何将相机结果保存到私人文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆