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
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)
.
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.
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">
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">
-------------------