How to prevent getExternalFilesDir (String type) from creating directory

616 views Asked by At

When i tried to get path from getExternalFilesDir(String type), the directory returned here will be automatically created. How to prevent this scenario. Please help me out ??

1

There are 1 answers

0
cygery On

When i tried to get path from getExternalFilesDir(String type), the directory returned here will be automatically created.

That's correct.

How to prevent getExternalFilesDir (String type) from creating directory

You can't.

How to prevent this scenario. Please help me out ??

I don't know of any good reason to try to prevent this from happening. Thus, I suggest that you reconsider your reasoning. Anyway, if you insist on preventing this scenario you'll have to:

  1. Manually determine the path of the external files directory for your app and supplied type. You can check how Android does this based on the source code of getExternalFilesDir. Please note that the linked source code is from the Android 5.0.0_r2 tag. I don't know if and what is different on other versions.
  2. Manually check if this directory exists. You can use the File class for this.
  3. Act accordingly, e.g., by not doing whatever you would do if the directory existed when it in fact does not.