Reputation: 31
I am using SAF to work with files on android, and I thought everything is working fine except that on a Huawei device running on EMUI 10 I found that files are being duplicated when I write on the output stream.
I construct manually the path to a document uri because when I use "findFile()" it takes minutes to iterate over all files I need (there are more than 1000). Comparing with File object takes less than 1 sec.
How do I construct a path Uri Manually
@Synchronized
fun getAsset(uuid: String): Uri =
Uri.parse("${assetsFolder!!.uri}%2F$uuid") // assetsFolder!!.findFile(uuid).uri takes minutes
Now on the below method, the method "exists" returns false and when the file is created, it returns a duplicate counter at the end.
@Synchronized
fun requireAsset(context: Context, uuid: String): DocumentFile {
val uri = getAsset(uuid)
val doc = toDocumentFile(context, uri)
return if (doc.exists())
doc
else {
return assetsFolder!!.createDirectory(MIME, uuid)!!
}
}
fun toDocumentFile(context: Context, uri: Uri): DocumentFile =
try {
DocumentFile.fromTreeUri(context, uri)!!
} catch (e: IllegalArgumentException) {
DocumentFile.fromFile(File(uri.path!!))
}
Since "exists()" returns false, I try to create a file with the specified UUID but the result would be duplicate file. When I check using the File Manager, I can see both files there (File A and File A (1)). Sometimes, "exists()" works properly I guess since I do not get a duplicate counter, but very rare. And this issue came up on an Huawei EMUI 10 Device.
requestLegacyExternalStorage is also enabled android:requestLegacyExternalStorage="true"
Upvotes: 3
Views: 257