Android getting external storage Absolute Path

送分小仙女□ 提交于 2019-12-13 06:17:25

问题


I want to download some files and save them into the internal storage of the phone/tab. Tried on Samsung Galaxy Note 2 and Galaxt Tab 10.1. When I use /storage/sdcard0/ on them the code runs successfully but when I use Galaxy Nexus 3 the code fails.

I want to get absolute path of the phoe or tab's internal storage' Absolute Path.

Does it possible, if so how?


回答1:


When I use /storage/sdcard0/ on them the code runs successfully but when I use Galaxy Nexus 3 the code fails.

/storage/sdcard0/ is not internal storage. It is external storage.

(BTW, since there is no device named "Galaxy Nexus 3", I am assuming that you simply meant "Galaxy Nexus")

To find locations on internal storage for your app, use getFilesDir(), called on any Context (such as your Activity, to get a File object.

To get a location on external storage unique for your app, use getExternalFilesDir(), called on any Context (such as your Activity, to get a File object.

To get a standard location on external storage for common types of files (e.g., movies), use getExternalStoragePublicDirectory() on Environment.

To get the root of external storage, use getExternalStorageDirectory() on Environment. This, however, is considered sloppy programming nowadays, as it encourages developers to just put files in random locations.




回答2:


why can't you use Environment.getExternalStorageDirectory() for getting /storage/sdcard0/ path from code ?

need to add permissions as well

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />



回答3:


The solution found is 1st edit the emulator:

  1. Go to Android Virtual Device Manager and then edit the emulator
  2. Set say 100 MB for SD d card for the respected emulator, say Ok
  3. Save and close emulator and start

It worked for me, the app is no longer giving an error and is working fine.



来源:https://stackoverflow.com/questions/32703442/populate-listview-with-mp3-files-found-locally

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!