Android中關於外部儲存的一些重要函式
public abstract File getExternalCacheDir ()
Returns the absolute path to the directory on the external filesystem (that is somewhere on Environment.getExternalStorageDirectory()
where
the application can place cache files it owns.
This is like getCacheDir()
in
that these files will be deleted when the application is uninstalled, however there are some important differences:
該函式像getCacheDir()
一樣,儲存在該目錄下的檔案會在解除安裝該應用程式時被刪除。然而又有以下幾點不同:
- The platform does not monitor the space available in external storage, and thus will not automatically delete these files. Note that you should be managing the maximum space you will use for these anyway, just
like with
getCacheDir()
. 系統並不會監視外部儲存的可用容量。因此程式設計師應該自己管理其佔用的最大空間,自己做檔案的清理機制 - External files are not always available: they will disappear if the user mounts the external storage on a computer or removes it. See the APIs on
Environment
for information in the storage state. - There is no security enforced with these files. All applications can read and write files placed here. 系統對該目錄下的檔案並沒做安全訪問機制,所有的應用程式都可以讀寫該目錄下的檔案。
Returns
- Returns the path of the directory holding application cache files on external storage. Returns null if external storage is not currently mounted so it could not ensure the path exists; you will need to call this method again when it is available.
public abstract File getExternalFilesDir (String type)
Returns the absolute path to the directory on the external filesystem (that is somewhere on Environment.getExternalStorageDirectory()
)
where the application can place persistent files it owns. These files are private to the applications, and not typically visible to the user as media.
該目錄下的檔案對其應用程式是私有的。其目錄下的檔案也不會被當做多媒體檔案而為使用者所見
This is like getFilesDir()
in
that these files will be deleted when the application is uninstalled, however there are some important differences:
該函式像 getFilesDir() 一樣,儲存在該目錄下的檔案會在解除安裝該應用程式時被刪除。然而又有以下幾點不同:
- External files are not always available: they will disappear if the user mounts the external storage on a computer or removes it. See the APIs on
Environment
for information in the storage state. - There is no security enforced with these files. All applications can read and write files placed here. 系統對該目錄下的檔案並沒做安全訪問機制,所有的應用程式都可以讀寫該目錄下的檔案。
Here is an example of typical code to manipulate a file in an application's private storage:
If you supply a non-null type to this function, the returned file will be a path to a sub-directory of the given type. Though these files are not automatically scanned by the media scanner, you can explicitly add them to
the media database with MediaScannerConnection.scanFile
.
Note that this is not the same asEnvironment.getExternalStoragePublicDirectory()
,
which provides directories of media shared by all applications. The directories returned here are owned by the application, and their contents will be removed when the application is uninstalled. UnlikeEnvironment.getExternalStoragePublicDirectory()
,
the directory returned here will be automatically created for you.
Here is an example of typical code to manipulate a picture in an application's private storage and add it to the media database:
Parameters
type |
The type of files directory to return. May be null for the root of the files directory or one of the following Environment constants for a subdirectory:DIRECTORY_MUSIC , DIRECTORY_PODCASTS , DIRECTORY_RINGTONES , DIRECTORY_ALARMS , DIRECTORY_NOTIFICATIONS , DIRECTORY_PICTURES ,
or DIRECTORY_MOVIES . |
---|
public static File getExternalStorageDirectory ()
Gets the Android 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.
In devices with multiple "external" storage directories (such as both secure app storage and mountable shared storage), this directory represents the "primary" external storage that the user will interact with.
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 by Context.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 by getExternalStoragePublicDirectory(String)
.
該函式現在已經不合時宜,在API Level 8後,對於應用程式的私有檔案應該放在Context.getExternalFilesDir目錄下,非私有的(shared)的檔案應該放在目錄下Environment提供的函式getExternalStoragePublicDirectory(String)所指定的目錄下
Here is an example of typical code to monitor the state of external storage:
Get a top-level public external storage directory for placing files of a particular type. This is where the user will typically place and manage their own files, so you should be careful about what you put here to ensure you don't erase their files or get in the way of their own organization.
Here is an example of typical code to manipulate a picture on the public external storage:
Parameters
type |
The type of storage directory to return. Should be one of DIRECTORY_MUSIC , DIRECTORY_PODCASTS , DIRECTORY_RINGTONES , DIRECTORY_ALARMS ,DIRECTORY_NOTIFICATIONS , DIRECTORY_PICTURES ,DIRECTORY_MOVIES , DIRECTORY_DOWNLOADS ,
or DIRECTORY_DCIM .
May not be null. |
---|
Returns
- Returns the File path for the directory. Note that this directory may not yet exist, so you must make sure it exists before using it such as with
File.mkdirs()
.
public static String getExternalStorageState ()
Gets the current state of the primary "external" storage device.
See getExternalStorageDirectory()
for
more information.
Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
String | MEDIA_BAD_REMOVAL |
getExternalStorageState() returns
MEDIA_BAD_REMOVAL if the media was removed before it was unmounted. |
|||||||||
String | MEDIA_CHECKING |
getExternalStorageState() returns
MEDIA_CHECKING if the media is present and being disk-checked |
|||||||||
String | MEDIA_MOUNTED |
getExternalStorageState() returns
MEDIA_MOUNTED if the media is present and mounted at its mount point with read/write access. |
|||||||||
String | MEDIA_MOUNTED_READ_ONLY |
getExternalStorageState() returns
MEDIA_MOUNTED_READ_ONLYif the media is present and mounted at its mount point with read only access. |
|||||||||
String | MEDIA_NOFS |
getExternalStorageState() returns
MEDIA_NOFS if the media is present but is blank or is using an unsupported filesystem |
|||||||||
String | MEDIA_REMOVED |
getExternalStorageState() returns
MEDIA_REMOVED if the media is not present. |
|||||||||
String | MEDIA_SHARED |
getExternalStorageState() returns
MEDIA_SHARED if the media is present not mounted, and shared via USB mass storage. |
|||||||||
String | MEDIA_UNMOUNTABLE |
getExternalStorageState() returns
MEDIA_UNMOUNTABLE if the media is present but cannot be mounted. |
|||||||||
String | MEDIA_UNMOUNTED |
getExternalStorageState() returns
MEDIA_UNMOUNTED if the media is present but not mounted. |
相關文章
- Android中關於內部儲存的一些重要函式2017-02-09Android函式
- Android 外部儲存2019-02-27Android
- mysql儲存函過程和儲存函式都屬於儲存程式2013-03-17MySql儲存函式
- Android-內部儲存和外部儲存2018-09-04Android
- 關於儲存及CSS的一些技巧2019-01-08CSS
- Android內部儲存和外部儲存,以及讀取Android讀取RAM,ROM內部儲存和外部儲存卡容量2017-12-02Android
- Android從外部儲存裝置中儲存和載入本地檔案2014-09-27Android
- 儲存函式2017-08-24儲存函式
- 關於變數的宣告和定義、內部函式和外部函式2024-11-14變數函式
- 函式索引的儲存2007-04-09函式索引
- Mysql 的儲存過程和儲存函式2013-08-02MySql儲存過程儲存函式
- 儲存過程與儲存函式2020-11-16儲存過程儲存函式
- 關於 Math.random 的一些函式2021-09-09random函式
- 關於虛擬函式的一些理解2024-06-27函式
- 關於儲存及RAC的一些好文章2009-09-10
- PL/SQL 中的儲存過程與函式2017-09-05SQL儲存過程函式
- 外部儲存器2024-08-08
- Oracle中關於函式的使用2007-06-18Oracle函式
- 關於qt中的tr()函式2013-03-29QT函式
- SQL SERVER 關於檔案儲存一些方法2013-04-03SQLServer
- mysql練習 —— 關於一些函式的使用2024-05-07MySql函式
- Android——儲存圖片到外部儲存並進行原生分享2017-12-21Android
- SQL中儲存過程和函式的區別2011-11-16SQL儲存過程函式
- 儲存過程 函式2015-04-11儲存過程函式
- SQL---------儲存函式2024-07-24SQL儲存函式
- (Oracle)儲存過程、儲存函式和包的相關知識與例項2011-12-02Oracle儲存過程儲存函式
- mysql和orcale的儲存過程和儲存函式2018-06-04MySql儲存過程儲存函式
- 關於cuda中的函式問題2018-03-06函式
- Android(Q)10 上的分割槽外部儲存訪問2020-02-24Android
- 外部函式的呼叫2018-11-06函式
- 關於字元函式的一些應用總結2015-04-05字元函式
- (9)mysql 中的儲存過程和自定義函式2017-01-18MySql儲存過程函式
- 一些主機儲存的開關2009-10-25
- 儲存過程與函式2017-12-21儲存過程函式
- MySQL 儲存函式及呼叫2024-06-10MySql儲存函式
- 關於MYSQL中FLOAT和DOUBLE型別的儲存2016-04-12MySql型別
- 可以利用的一些系統儲存過程、檢視、函式2009-09-01儲存過程函式
- 關於 js 中的回撥函式 callback2019-03-04JS函式