Android Environment.getExternalStorageDirectory() 獲取的是內部存儲還是外部存儲?


這幾天在做Android應用的遠程更新功能,將下載的更新包放在移動設備上指定的文件夾。

用的是  Environment.getExternalStorageDirectory() 這種方法。然后在獲取的文件夾中新建一個hkapp文件夾,用來存放下載的apk文件。

那么,這個hkapp文件究竟是在那塊存儲區域呢?

一開始,看看網上的API,已經這種方法的字面意思。想當然地以為它就是獲取SD卡上的文件夾,而不是手機的內部存儲。

當然。除了望文生義之外,似乎還有確鑿的證據支持我的觀點。那就是在執行的時候報錯,提示權限不足,也就是要配置訪問外部存儲的權限:

[html]  view plain  copy
  在CODE上查看代碼片 派生到我的代碼片
  1. <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>  
  2. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>  
嗯,看上去就是在獲取SD卡了...


我有兩個手機:

1. 華為C8812,帶SD卡,接上電腦之后顯示有兩個存儲設備,而且識別為磁盤。當中。H盤是手機自帶的,而I盤就是后來放進去的SD卡。



在程序中用了getExternalStorageDirectory()方法之后,發現hkapp目錄的實際位置是在I盤,也就是SD卡上,OK,看上去這個getExternalStorageDirectory方法確實是獲取了SD卡。而不是手機自帶的存儲。


2. 華為C8817,不帶SD卡,接上電腦之后僅僅顯示一個設備,而且,是作為設備總體來識別,而不單單是個磁盤。




在這個C8817上執行程序之前。我是有點小操心的,由於這個手機沒有SD卡啊,會不會執行到一半報錯呢?那么實際的情況是,確實報錯了,但報的是沒有權限訪問外部存儲的錯,於是也把權限加上去:

[html]  view plain  copy
  在CODE上查看代碼片 派生到我的代碼片
  1. <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>  
  2. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>  
然后就想了,給了你權限又有什么用呢?反正你終究是沒有SD卡在里面。必定要繼續報錯嘛。於是又一次執行,發現整個下載過程一切正常,沒有報錯。hkapp目錄也正常建立了。但位置是在我原本以為的”內部存儲“中,apk文件也順利地進去了。

那么,到這里,有點錯亂了,這個”getExternalStorageDirectory()“ 究竟是獲取外部存儲還是內部存儲呢?



事實上這兩個存儲都是外部儲存,真正的內部儲存位置是data/data/包名



官方推薦 使用getExternalFilesDir(String) or getExternalCacheDir()而且不須要申請權限

使用getExternalFilesDir(String) 存儲數據時候。系統在刪除應用時,會把里面的數據也清楚,這種話,里面的內容就不會長期留在手機里。


Return the primary external storage directory. This directory may not currently be accessible if it has been mounted by the user on their computer, has been removed from the device, or some other problem has happened. You can determine its current state with getExternalStorageState().

Note: don't be confused by the word "external" here. This directory can better be thought as media/shared storage. It is a filesystem that can hold a relatively large amount of data and that is shared across all applications (does not enforce permissions). Traditionally this is an SD card, but it may also be implemented as built-in storage in a device that is distinct from the protected internal storage and can be mounted as a filesystem on a computer.

On devices with multiple users (as described by UserManager), each user has their own isolated external storage. Applications only have access to the external storage for the user they're running as.

In devices with multiple "external" storage directories, this directory represents the "primary" external storage that the user will interact with. Access to secondary storage is available through

Applications should not directly use this top-level directory, in order to avoid polluting the user's root namespace. Any files that are private to the application should be placed in a directory returned byContext.getExternalFilesDir, which the system will take care of deleting if the application is uninstalled. Other shared files should be placed in one of the directories returned bygetExternalStoragePublicDirectory(String).

Writing to this path requires the WRITE_EXTERNAL_STORAGE permission, and starting in read access requires the READ_EXTERNAL_STORAGE permission, which is automatically granted if you hold the write permission.

Starting in KITKAT, if your application only needs to store internal data, consider using getExternalFilesDir(String) or getExternalCacheDir(), which require no permissions to read or write.

This path may change between platform versions, so applications should only persist relative paths.






免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM