Search code examples
javaandroidfileandroid-internal-storage

Cannot find the created .txt file in android local storage


Summary

When creating the txt file in android's local or internal storage, file explorer is not able to find the created file.

Description

I have an app which sotores the array list to txt file and reads it back when it needed.

Function to write the file

....
 private void writeToFile(ArrayList<ImageModel> arrayList, String Filename) {
        try {
            FileOutputStream outputStreamWriter = requireContext().openFileOutput(Filename, Context.MODE_PRIVATE);

            ObjectOutputStream oos = new ObjectOutputStream(outputStreamWriter);
            Log.d(TAG, "writeToFile: arraylist "+arrayList.size());
            oos.writeObject(arrayList);
            oos.close();
            outputStreamWriter.close();
        } catch (IOException e) {
            Log.e("Exception", "File write failed: " + e.toString());
        }
    }
....

function to read from file

....

    private Object readFromFile(String Filename) {

        try {
            FileInputStream fis = requireContext().openFileInput(Filename);
            ObjectInputStream ois = new ObjectInputStream(fis);
            Object object = ois.readObject();
            return object;
        } catch (Exception ex) {
            Log.d(TAG, "readFromFile: exception "+ex.toString());
            return null;
        }
    }
....

So, far everything is working fine. It is writing and reading the file. But, I cannot find the same txt file in the device explorer or file manager. According to docs, by default openFileOutput will create a file inside Android/data/packagename/files. But, file explorer is not showing the dir either.

But, App is writing and reading the file. When I printed size of object returned by readFromFile() It printed the expected size. But, I can't find the file inside file explorer.

Things I tried

  1. I tried to get write and read permission. But, as android 10+ doesn't need any permission for writing or reading in ScopedStorage It didn't effect anything.

  2. I tried with creating the dir using requireContext().getExternalFilesDir(null) which created the files directory inside Android folder and then I tried to write and read the file. But, Dir is empty.

  3. I even tried checking show hidden files options in file explorer. But, no luck.


Solution

  • According to docs, by default openFileOutput will create a file inside Android/data/packagename/files

    No, it will not. openFileOutput() writes to the same location as you get via getFilesDir(), which is what the Android SDK refers to as internal storage.

    But, file explorer is not showing the dir either.

    Only your app has access to your portion of internal storage. Using development tools, Android Studio's Device File Explorer can show you internal storage for debuggable apps, such as debug builds of your app.

    I tried with creating the dir using requireContext().getExternalFilesDir(null) which created the files directory inside Android folder and then I tried to write and read the file. But, Dir is empty.

    On Android 10+, other apps do not have access to your app-specific locations on what the Android SDK refers to as external storage.