-1

Some one solve my code

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 Its return true if exists, but Above Q it return always false

Ryan M
  • 18,333
  • 31
  • 67
  • 74
Tarif Chakder
  • 1,708
  • 1
  • 11
  • 10

1 Answers1

-1

Please try the below code.

<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
    }

I hope it works for you!!

Sanjay Chauhan
  • 893
  • 7
  • 13
  • Environment.getExternalStorageDirectory() depreciated api 29 . The mentioned code working bellow 29 after update api above it returns false – Tarif Chakder Sep 01 '21 at 12:40
  • please refer this https://stackoverflow.com/questions/57116335/environment-getexternalstoragedirectory-deprecated-in-api-level-29-java – Sanjay Chauhan Sep 01 '21 at 12:52