getExternalFilesDir返回不一致的目录

我有一个操作,允许用户下载图像,并将其保存到我的应用程序的数据目录。

我创建临时文件,如下所示:

@Throws(IOException::class) fun Context.createPrivateMediaFile(prefix: String, extension: String): File { val timeStamp = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(Date()) val imageFileName = "${prefix}_${timeStamp}_" val storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES) return File.createTempFile(imageFileName, extension, storageDir) } 

我就这样使用它:

 try { photoFile = createPrivateMediaFile(".png") } catch (e: IOException) { errorRef = e } finally { if (photoFile == null) { callback(null) } else { tempFilePath = photoFile.absolutePath Ld("Temp image path $tempFilePath") // File created; proceed with request val photoURI = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".provider", photoFile) photoFile.outputStream().use { resource.compress(Bitmap.CompressFormat.PNG, 100, it) } callback(photoURI) } } 

目的是在我的数据目录中创建一个新文件,保存文件,然后通过回调发出uri。

这种方法大部分时间都在工作,但是我得到了uri无法解析的报告。 在这些情况下,打印的错误是:

Failed to find configured root that contains /storage/emulated/0/Android/data/com.pitchedapps.frost/files/Fotos/Frost_20170819_111929_1351301627.png

我的文件提供程序包含以下路径:

 <external-path name="Frost_images" path="Android/data/com.pitchedapps.frost/files/Pictures" /> 

所以Fotos文件夹从哪里来就很奇怪 这个错误也发生过一次,所以不应该是一个本地化的问题。

那么为什么getExternalFilesDir似乎返回不同的路径? 有没有办法获得一个确保保持一致的基础数据目录?

又一个例子是以下路径错误:

Failed to find configured root that contains /data/data/com.pitchedapps.frost/cache/Frost_20170819_115054_2013474631.png

附在下面是我的

完整的活动源代码

和我的

媒体使用情况