2

I want to create a text file on my android phone and write some text into it.
When I click on the button it says saved to /data/user/0/com.example.savetotextfile/files/example.txt
But I can't find these folders.
Most apps are in Device storage > Android > data, but the apps that I created myself are not in there.
Am I missing a permission?
I don't want to write to an sd card.

public class MainActivity extends AppCompatActivity {

    private static final String FILE_NAME = "example.txt";

    EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText = findViewById(R.id.editText);
    }

    public void save(View v) {
        String text = editText.getText().toString();
        FileOutputStream fos = null;

        try {
            fos = openFileOutput(FILE_NAME, MODE_PRIVATE);
            fos.write(text.getBytes());

            editText.getText().clear();
            Toast.makeText(this, "Saved to" + getFilesDir() + "/" + FILE_NAME, Toast.LENGTH_LONG).show();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
Quick learner
  • 10,632
  • 4
  • 45
  • 55
theerrormagnet
  • 174
  • 2
  • 15
  • what do you mean by this 'But I can't find these folders. ' ? – Quick learner Apr 12 '20 at 10:44
  • @Quicklearner When I take my phone and go to Device storage. I can't find a folder for my application or the text file. The same happens when connecting the phone to the PC. Maybe it is because of "MODE_PRIVATE", but then which mode do I use? – theerrormagnet Apr 12 '20 at 12:08
  • check my answer if you want to see files saved of your app in internal storage – Quick learner Apr 12 '20 at 12:39

1 Answers1

6

If you want to see file saved in internal storage you cannot just see through file manager. (Note your phone should be rooted if you want to see that directly)

Follow these steps to view files saved in internal storage.

1) Attach the device to your laptop/computer.

2) Open the android studio.

3) Now in the right extreme of android studio you will this option Device File Manager.

enter image description here

4) Double click on data folder then again double click on data folder

enter image description here

5) find the package name of your app like com.example.yourapp and double click on it.

6) double click on files folder under the package name of your app you want to see the internal storage files. enter image description here

Quick learner
  • 10,632
  • 4
  • 45
  • 55
  • This is a great answer, but when I click on my app there is the following red message appearing under it: ```run-as: Could not set capabilities: Operation not permitted``` – theerrormagnet Apr 12 '20 at 12:54
  • that depends on device my dear , Samsung devices sometimes show this error, please change charging via usb when you connect the device . – Quick learner Apr 12 '20 at 12:56
  • 1
    one important thing , you can only view files of those package names who are installed in that device by your android studio , means you cannot see files of other package names which aren't installed by your android studio – Quick learner Apr 12 '20 at 12:58
  • https://stackoverflow.com/questions/47390411/android-studio-3-0-device-explorer-run-as-could-not-set-capabilities-operati – Quick learner Apr 12 '20 at 13:00
  • Changing the USB options did not make a difference. – theerrormagnet Apr 12 '20 at 13:34
  • make sure you can only see those apps data which are installed by your studio dear ,debuggable apps – Quick learner Apr 12 '20 at 14:11
  • Well, my phone is not rooted and that is probably the reason why it is just not possible to view the data folder. I misunderstood that first part of the post. – theerrormagnet Apr 13 '20 at 15:01
  • Well that's a not an issue , please change usb options to usb storage ,charging or media storage – Quick learner Apr 13 '20 at 15:11
  • Changing the usb option on my phone does not change anything. The error message still appears. I even tried it with adb and another usb cable. I can't access the data folder. – theerrormagnet Apr 13 '20 at 22:01
  • Ok, so it could actually see the internal files of my app on my newer android phone with android studio, but I can not see them on my older phone. – theerrormagnet Apr 13 '20 at 22:46
  • I am glad you are able to see now , yeah it happens with some devices especially Samsung – Quick learner Apr 14 '20 at 12:56