1

I'm calling for your help, I'm stuck for my android project, and I can't find a solution.

Explanation :

I retrieve an .ics (iCalendar) file from a URL on the internet with the Apache Common library using FileUtils.copyURLToFile(url, file). It works very well, my file is created with my data and it is readable.

I save my file in the path : /storage/emulated/0/Android/data/MyApplication/MyFile.ics.

I can see it in the files of the phone (Android Pie) and/or the emulator (Android Oreo) at this address. So it is well created and present.

But when I want to parse my file with the iCal4j library, I get an error with the line containing FileInputStream, telling me that my file or directory does not exist at this address.

EDIT I specify that I check that my file exists with file.exist(). If it exists then it calls the function to parse my file. Where I have a problem with the FileInputStream. So yes, file.exist() tells me that it exists.

    File file = new File(Environment.getExternalStorageDirectory() + "/Android/data/MyApplication/MyFile.ics");

    FileInputStream fin = new FileInputStream(file.getPath());

    CalendarBuilder builder = new CalendarBuilder();

    Calendar calendar = builder.build(fin);

I get one mistake.

My errors :

W/System.err: java.io.FileNotFoundException: /storage/emulated/0/Android/data/MyApplication/MyFile.ics (No such file or directory)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:200)
    at java.io.FileInputStream.<init>(FileInputStream.java:150)
    at java.io.FileInputStream.<init>(FileInputStream.java:103)

My manifest.xml :

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

and I also asked the user's permission to access the storage:

requestPermissions(new String[]{WRITE_EXTERNAL_STORAGE,READ_EXTERNAL_STORAGE}, 1);

I hope I was clear enough to get help, thank you.

jylsdev
  • 11
  • 1
  • 3
  • Use file.exists() before you open the stream. Return if it does not exist. – blackapps May 28 '20 at 18:24
  • I do the file.exist(), I forgot to mention that. And he returns his existence to me. – jylsdev May 28 '20 at 19:10
  • Then put that code in your post on the right place. Show us exactly what you do. Do not only talk that you do. – blackapps May 28 '20 at 21:41
  • According to docs, "Return the primary shared/external storage directory. This directory may not currently be accessible if it has been mounted by the user on their computer, has been removed from the device, or some other problem has happened. You can determine its current state with `getExternalStorageState()`." Did you tried in the unmounted state? – cgb_pandey May 29 '20 at 02:31

3 Answers3

1

try with following code File file = new File(Environment.getExternalStorageDirectory() + "/MyApplication/MyFile.ics");

Thirumalai
  • 176
  • 9
1

If all the permissions are requested, it will still prompt that the permission request failed. It's probably the adaptation problem of Android 10.0. You can add it under

    <application
       ...
    android:requestLegacyExternalStorage="true"
       ...
        >

https://developer.android.com/training/data-storage/use-cases

jacksinrow
  • 49
  • 4
1
File file = new File(Environment.getExternalStorageDirectory()+"/Android/data/MyApplication/MyFile.ics");
if(file.createNewFile(){
   FileInputStream fin = new FileInputStream(file.getPath());
   // Your entire code
}
else{
   //File exist already Handle it
 }

TIP:

  1. Replace (it is not mandatory just to reduce size of code) Environment.getExternalStorageDirectory()+"/Android/data/MyApplication/MyFile.ics to getActivity().getExternalFilesDir(null)+"/MyFiles.ics"
  2. No need Read and Write permission to Read & write in your persistent folder so remove it if possible.

AND if your app target above or equal to Android 10 then added android:requestLegacyExternalStorage="true" inside application Tag

Like :

    <application
    ....
    android:requestLegacyExternalStorage="true"
    ....>
Udhaya
  • 335
  • 3
  • 12