android - 拍照并保存到内部存储并获取 getUriForFile 错误

标签 android android-camera android-fileprovider android-storage

所以我正在开发一个应用程序,我会在其中拍照并尝试将其保存到应用程序的内部存储空间中。我在使用文件提供程序时遇到问题。我已经看过许多关于堆栈溢出的问题,但如果可能的话,我想得到更详细的解释。

我也按照谷歌的例子,它给了我以下错误。 https://developer.android.com/training/camera/photobasics

Failed to find configured root that contains /storage/emulated/0/Android/data/com.myapp.debug/files/Pictures/JPEG_20180427_095752_2090822261.jpg

以下是我按照 Google 的示例使用的代码。

<provider
        android:name=".application.blinkupregistration.postworkphotos.PostWorkPhotosFileProvider"
        android:authorities="${applicationId}.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
    </provider>

在我的代码中。

Uri photoURI = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".provider", photoFile);

对于以上两个,我还尝试将 com.myapp.provider 硬编码到 authorities 和 getUriForFile 方法中。还为 getUriForFile 方法做了 getpackageName()。但是这些并没有太大的改变。我认为主要问题是路径。

使用 Google 的示例尝试了以下路径,

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="post_work_photos" path="Android/data/${applicationId}/files/Pictures" />
</paths>


<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="post_work_photos" path="Android/data/com.myapp/files/Pictures" />
</paths>

每当我将 paths.xml 更改为以下内容时,我都会使用它。

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="post_work_photos" path="." />
</paths>

但我不明白为什么它适用于句点。我也不知道这是否是正确的做法,这是我主要关心的问题。

如果有人能帮助我,那就太好了。谢谢。

最佳答案

Tried the following paths by using Google's example,

他们的例子……有问题。

But I do not understand why it works with the period.

因为你是说你愿意支持外部存储根目录里面的任何东西。

I also do not know if this the correct practice

您可以使用 <external-files-path> 稍微缩小范围:

<external-files-path name="post_work_photos" path="." />

现在,您只提供 getExternalFilesDir() 中的文件,这是您的文件指向的位置。

关于android - 拍照并保存到内部存储并获取 getUriForFile 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50064200/

相关文章:

android - 在 viewPager 中的 fragment 之间滑动时,选项卡高光不会改变

android - 某些时候相机对象在初始化时为空

android - 屏幕旋转时保持图片位置

android - 如何有意识地启动前置摄像头?

在 android Kitkat 中找不到 android.support.v4.content.FileProvider 类

android - 在 Android 应用程序中添加多个文件提供程序

android - 为 FileProvider 动态生成文件路径

启动 Activity 时 Android 屏幕闪烁

android - 三星浏览器无法下载 .ics 文件以添加到日历

xml - Android 新手 - 在运行时绘制 View