SoFunction
Updated on 2025-04-09

Android file storage and FAQ solution

Android file storage

Check out the file storage routines you can search everywhere on the Internet

if(Environment.MEDIA_MOUNTED.equals(())){
        cacheDir=().getPath();
      }else {
        cacheDir=().getPath();
      }
 fileOutputStream=new FileOutputStream(cacheDir+"/"+url);

This code is definitely not wrong, but it deceived many young developers.

When you store the file like this, it will report that the file does not exist. You start to think that this path was obtained by me, but it was not written by me. How could it not exist? But the acquisition is only logical existence, not real existence. So we must create its folder and create this file when the file does not exist. Therefore, the standard writing method should be

private void initFile() {
    if(Environment.MEDIA_MOUNTED.equals(())){
      cacheDir=().getPath();
      (TAG,"have SD");
    }else {
      cacheDir=().getPath();
      (TAG," not have SD");
    }
    (TAG,cacheDir);
    File file=new File(cacheDir);
    if(!()) {
      (TAG, "file no exists");
      ();
    }
  }

In this way, if that folder does not exist, create it and create it recursively, and this problem will be solved perfectly.

Thank you for reading, I hope it can help you. Thank you for your support for this site!