Check for file existence in androids assets folder?

前端 未结 6 1250
不思量自难忘°
不思量自难忘° 2020-12-30 19:33

My app has .txt files in subdirectories in the assets folder. It reads those .txt files and puts them in a textview. It\'s working great and no problems.

Should I be

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-30 19:57

    if you really want to check for the file existence:

    AssetManager mg = getResources().getAssets();
    InputStream is = null;
    try {
      is = mg.open(pathInAssets);
      //File exists so do something with it
    } catch (IOException ex) {
      //file does not exist
    } finally {
      if (is != null) {
        is.close();
      }
    }
    

    If your file is located in assets/folder/file.ext, then pathInAssets would be "folder/file.ext"

提交回复
热议问题