Environment.getExternalStorageDirectory().getAbsolutePath() gives a different path - Android

前端 未结 2 871
故里飘歌
故里飘歌 2020-12-16 01:39

I try to write something into my phone memory.

At first, I hard-coded the path as:

myFile = new File(\"/sdcard/\" + txtName.getText() + \".txt\");


        
相关标签:
2条回答
  • 2020-12-16 02:12

    Why would this happen?

    Because the path to external storage has changed over the years, which is why you should have never hard-coded the path in the first place.

    how can I get the path directory in a more "formal and right" way instead of hardcoding the path?

    Use Environment.getExternalStorageDirectory().

    On your test environment, /sdcard is a symlink or hardlink to /storage/sdcard0, as set up by the device manufacturer. However, there is no guarantee that all devices will have such an /sdcard symlink.

    I guess the 32GB storage comes with a built-in SD card

    External storage is a portion of your on-board 32GB of flash memory.

    0 讨论(0)
  • 2020-12-16 02:25

    Remove the .getAbsolutePath() and it will be fine. Environment.getExternalStoreDirectory() will give you the path to wherever the manufacture has set their external storage.

    0 讨论(0)
提交回复
热议问题