java.io.FileNotFoundException, open failed: ENOENT

前端 未结 4 1077
时光取名叫无心
时光取名叫无心 2020-12-21 16:31

For some reason I am getting a fileNotFoundException for both the times I read. Something worth noting is that the Toast prints \"File exists!\". I used the

相关标签:
4条回答
  • 2020-12-21 17:01

    getFilesDir returns a File object. If you call onString on it (which you do implicitly), it returns its path. The path is not ending with a slash if the file is a directory, so getActivity().getFilesDir()+filename will result in something like "/data/data/com.yourapp/filescalendar_recipes.txt".

    You can either use getActivity().getFilesDir()+File.separator+filename, or just call new FileInputStream(file).

    0 讨论(0)
  • 2020-12-21 17:01

    You have already created a File instance with File file = getActivity().getFileStreamPath(filename); which is the instance that you are checking with the file.exists() method. Then you are trying to read another thing with the FileInputStream. You should try FileInputStream fileInputStream = new FileInputStream(file);. With that you are creating your stream with the file that you already checked.

    0 讨论(0)
  • 2020-12-21 17:05

    Newer version of android sometimes don't support creating folders ( very strange for me, but I experienced it), then :-

    1- be sure that folder is created and or/ 2- add this to mainfests <application android:requestLegacyExternalStorage="true" tools:targetApi="q">

    0 讨论(0)
  • 2020-12-21 17:12

    I was Getting the same error. This Answer worked for me.

    You just have to add one line in your manifest like this:

    <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:requestLegacyExternalStorage="true" //Add this Line
    android:label="@string/app_name">
    

    -------------------

    0 讨论(0)
提交回复
热议问题