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.






注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com