before calling FileOutputStream() i am calling this public void createDirectory() { File directory = new File(getFilesDir() + "/training"); if (!directory.exists()) { directory.mkdir(); } }
That is not where you are trying to write the file:
String root = Environment.getExternalStorageDirectory().toString(); String path = path = "/training/training.pdf"; String fileName = fileName=root+"/"+getFilesDir()+path;
As you can see, these do not match.
If you want the file on external storage, the best solution is to get rid of all of that and use getExternalFilesDir()
, called on Context
(akin to getFilesDir()
). This gives you:
- A unique directory
- On external storage
- Where the files will be deleted when your app is uninstalled
- That does not require permission on Android 4.4+
The next-best solution for external storage is to get rid of all of that and use Environment.getExternalStoragePublicDirectory()
as your base, to put the files in a standard location based on their type (e.g., photos, videos, music). These files will survive an uninstall, and this location will require WRITE_EXTERNAL_STORAGE
. Plus, you will have to create a unique subdirectory under this root yourself, though typically this is done via a user-friendly display name (e.g., OpenCamera
) rather than the package name.
Your directory that you are creating in createDirectory()
is perfectly fine, but it is internal storage, so the user cannot access it. If you want to use that, then use the same algorithm to create the File
for your FileOutputStream
as you are using in createDirectory()
.