Android获取外部存储绝对路径

时间:2022-02-23 16:49:21

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.

我想下载一些文件并保存到phone/tab的内部存储中。试用三星Galaxy Note 2和Galaxy Tab 10.1。当我使用/存储/sdcard0/ on时,代码运行成功,但当我使用Galaxy Nexus 3时,代码就失败了。

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

我想要得到phoe或tab的内部存储的绝对路径。

Does it possible, if so how?

如果可能的话,怎么可能呢?

3 个解决方案

#1


26  

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

当我使用/存储/sdcard0/ on时,代码运行成功,但当我使用Galaxy Nexus 3时,代码就失败了。

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

/存储/sdcard0/不是内部存储。它是外部存储器。

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

(顺便说一句,既然没有一款设备叫做“Galaxy Nexus 3”,我假设你的意思只是“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.

要找到应用程序内部存储的位置,可以使用getFilesDir(),它在任何上下文(比如您的活动)上调用,以获取文件对象。

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.

要在应用程序的外部存储上获得惟一的位置,可以使用getExternalFilesDir(),它可以调用任何上下文(例如您的活动)来获取文件对象。

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

要获取常用文件类型(如电影)的外部存储的标准位置,请在环境上使用getExternalStoragePublicDirectory()。

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.

要获取外部存储的根,请在环境上使用getExternalStorageDirectory()。然而,这在现在被认为是草率的编程,因为它鼓励开发人员将文件放在随机的位置。

#2


10  

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

为什么不能使用Environment.getExternalStorageDirectory()从代码中获取/存储/sdcard0/路径?

need to add permissions as well

还需要添加权限

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

#3


1  

The solution found is 1st edit the emulator:

找到的解决方案是首先编辑模拟器:

  1. Go to Android Virtual Device Manager and then edit the emulator
  2. 转到Android虚拟设备管理器,然后编辑模拟器。
  3. Set say 100 MB for SD d card for the respected emulator, say Ok
  4. 设置为SD d卡的100mb为受尊敬的模拟器,点击Ok。
  5. Save and close emulator and start
  6. 保存并关闭仿真器并启动。

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

这款应用对我来说很有用,它不再出错,而且运行良好。

#1


26  

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

当我使用/存储/sdcard0/ on时,代码运行成功,但当我使用Galaxy Nexus 3时,代码就失败了。

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

/存储/sdcard0/不是内部存储。它是外部存储器。

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

(顺便说一句,既然没有一款设备叫做“Galaxy Nexus 3”,我假设你的意思只是“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.

要找到应用程序内部存储的位置,可以使用getFilesDir(),它在任何上下文(比如您的活动)上调用,以获取文件对象。

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.

要在应用程序的外部存储上获得惟一的位置,可以使用getExternalFilesDir(),它可以调用任何上下文(例如您的活动)来获取文件对象。

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

要获取常用文件类型(如电影)的外部存储的标准位置,请在环境上使用getExternalStoragePublicDirectory()。

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.

要获取外部存储的根,请在环境上使用getExternalStorageDirectory()。然而,这在现在被认为是草率的编程,因为它鼓励开发人员将文件放在随机的位置。

#2


10  

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

为什么不能使用Environment.getExternalStorageDirectory()从代码中获取/存储/sdcard0/路径?

need to add permissions as well

还需要添加权限

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

#3


1  

The solution found is 1st edit the emulator:

找到的解决方案是首先编辑模拟器:

  1. Go to Android Virtual Device Manager and then edit the emulator
  2. 转到Android虚拟设备管理器,然后编辑模拟器。
  3. Set say 100 MB for SD d card for the respected emulator, say Ok
  4. 设置为SD d卡的100mb为受尊敬的模拟器,点击Ok。
  5. Save and close emulator and start
  6. 保存并关闭仿真器并启动。

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

这款应用对我来说很有用,它不再出错,而且运行良好。