【问题标题】:File path exist always return false Android Above Q文件路径存在总是返回 false Android Above Q
【发布时间】:2021-09-01 12:00:28
【问题描述】:

有人解决了我的代码

private fun isFileExists(): Boolean {
    var checked = false
    try {
        val filePath= this.applicationInfo.dataDir + PATH_SUFFIX + FILE_NAME
        val file = File(filePath)
        if (file.exists()) {
            checked = true
        }
    } catch (ignored: Exception) {
    }
    return checked
}

Bellow android Q 如果存在则返回true,但在Q之上它总是返回false

【问题讨论】:

  • 在 Android 10 及更高版本上,您无权访问其他应用的外部存储部分。
  • 是的,我知道,但是,我想找到我自己的应用程序 this.applicationInfo.dataDir
  • 请告知完整路径。告诉 fullPath 的值。我们应该怎么知道?

标签: android performance android-fragments scoped-storage


【解决方案1】:

请尝试以下代码。

<manifest ... >
<!-- This attribute is "false" by default on apps targeting
     Android 10 or higher. -->
  <application android:requestLegacyExternalStorage="true" ... >
    ...
  </application>
</manifest>


private fun isFileExists(): Boolean {
        var checked = false
        try {
            val privateTempDir = Environment.getExternalStorageDirectory()

            val filePath= privateTempDir.absolutePath + PATH_SUFFIX + FILE_NAME
            val file = File(filePath)
            if (file.exists()) {
                checked = true
            }
        } catch (ignored: Exception) {
            ignored.printStackTrace()
        }
        return checked
    }

希望对你有用!!

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2013-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-20
  • 1970-01-01
相关资源
最近更新 更多