1

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.

1 Answers1

2

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • This makes sense. So, how do i save the file to specified location? lets say i want to save it to `emulated/0/myApp` . How I can save the file to this location? outside of `Android` folder. And can I post comment here for my future questions to ask your help? – Anirudhdhsinh Jadeja Jul 09 '21 at 11:03
  • @AnirudhdhsinhJadeja: "How I can save the file to this location? " -- you can't on Android 11+. I recommend that you let the *user* decide where on the *user's* device (or the *user's* chosen cloud service provider) you should store the *user's* content. You can do that via the Storage Access Framework, using things like `ACTION_CREATE_DOCUMENT` and `ACTION_OPEN_DOCUMENT_TREE`. – CommonsWare Jul 09 '21 at 12:09
  • @AnirudhdhsinhJadeja: "And can I post comment here for my future questions to ask your help?" -- you are welcome to do whatever you want within the site rules. Questions get greater visibility than do comments, though. – CommonsWare Jul 09 '21 at 12:11
  • Thanks. I understand Due to security reasons app is only allowed to use it's internal storage. For all the other purposes user must save files to cloud. Got it. – Anirudhdhsinh Jadeja Jul 09 '21 at 12:13
  • 1
    @AnirudhdhsinhJadeja: "For all the other purposes user must save files to cloud" -- no, but the user needs to be involved for storing in arbitrary locations, and on the whole apps cannot see other apps files just via the filesystem. [This 14-post series](https://commonsware.com/blog/2019/10/19/scoped-storage-stories-saf-basics.html) outlines using the Storage Access Framework and `MediaStore`. – CommonsWare Jul 09 '21 at 12:16
  • hey, I cannot find the suitable answer for this question. https://stackoverflow.com/questions/68390421/how-to-select-multiple-items-in-recycler-view-android can you help me? – Anirudhdhsinh Jadeja Jul 16 '21 at 09:10