天天看點

Saving Files 儲存檔案Saving Files

儲存檔案

                   選擇内部或者外部存儲      

            為外部存儲擷取權限

                           在内部存儲儲存一個檔案

                          在外部存儲儲存一個檔案

                                               查詢空閑空間                                          

                                                        删除檔案

您還應該閱讀

          使用内部存儲

         使用外部存儲

Android uses a file system that‘s similar to disk-based file systems on other platforms. This lesson describes how to work with the Android file system to read and write files with the

<code></code> APIs.

安卓系統使用的檔案系統類似于其他平台上的基于磁盤的檔案系統。這節課描述了Android如何使用檔案系統來讀取和寫入<code></code>的API。

A <code></code> object is suited to reading or writing large amounts of data in start-to-finish order without skipping around. For example,

it‘s good for image files or anything exchanged over a network.

一個對象是适合于從頭到尾的順序不跳過地閱讀或編寫大量的資料提供。例如,有利于圖像檔案或任何通過網絡交換的檔案。

This lesson shows how to perform basic file-related tasks in your app. The lesson assumes that you are familiar with the basics of the Linux file system and the standard file input/output APIs in

<code></code>.

這節課展示了如何在您的應用程式中執行基本與檔案相關的任務。這個課程可以使您熟悉基本的Linux檔案系統和在in

<code></code>中的标準檔案輸入/輸出的API。

選擇内部或者外部存儲   

All Android devices have two file storage areas: "internal" and "external" storage.  These names come from the early days of Android, when most devices offered built-in non-volatile memory (internal storage), plus a removable

storage medium such as a micro SD card (external storage). Some devices divide the permanent storage space into "internal" and "external" partitions, so even without a removable storage medium, there are always two storage spaces and the API behavior is the

same whether the external storage is removable or not. The following lists summarize the facts about each storage space.

所有的Android裝置有兩個檔案存儲區域:“内部”和“外部”存儲。它們名字來自Android的早期,當大多數裝置提供了内置的非易失性存儲器(記憶體),加上一個可移動的存儲媒體如微型SD卡(外部存儲)。一些裝置将永久存儲空間劃分為“内部”和“外部”分區,是以即使沒有一個可移動的存儲媒體,總有兩種存儲空間和API行為是相同的外部存儲器是否可移動。以下清單總結關于每個存儲空間的事實。

Internal storage:

記憶體

It‘s always available.        它總是可用的。

Files saved here are accessible by only your app by default.    檔案儲存在預設情況下隻可以通路應用程式。  

When the user uninstalls your app, the system removes all your app‘s files from internal storage.  

當使用者解除安裝應用程式,從内部存儲的檔案系統會删除所有你的應用。

Internal storage is best when you want to be sure that neither the user nor other apps can access your files.

當你想確定無論是使用者還是其他應用程式可以通路你的檔案時内部存儲是最好。

External storage:

外部存儲

It‘s not always available, because the user can mount the external storage as USB storage and in some cases remove it from the device.

         它并不總是可用的,因為使用者可以安裝外部存儲USB存儲和在某些情況下從裝置上删除它。

It‘s world-readable, so files saved here may be read outside of your control.

         這是公開的,是以儲存在這裡的檔案可以在你的控制之外閱讀。

When the user uninstalls your app, the system removes your app‘s files from here only if you save them in the directory from

         當使用者解除安裝應用程式時,隻有從中儲存檔案,系統才能删除應用程式的檔案.

External storage is the best place for files that don‘t require access restrictions and for files that you want to share with other apps or allow the user to access with a computer.

Tip: Although apps are installed onto the internal storage by default, you can specify the

attribute in your manifest so your app

may be installed on external storage. Users appreciate this option when the APK size is very large and they have an external storage space that‘s larger than the internal storage. For more information, see

.

外部存儲是存儲檔案最好的地方,不需要通路限制,你想與其他應用程式共享的檔案或允許使用者通路都可以。

提示:盡管應用程式安裝到内部存儲在預設情況下,您可以指定屬性在你的清單,這樣你的應用程式可以安裝在外部存儲器。使用者欣賞這個選項當APK檔案大小是非常大的,他們有一個外部比内部存儲的存儲空間。有關更多資訊,請。

為外部存儲擷取權限

To write to the external storage, you must request the  

<code></code> permission in your

:

寫外部存儲,您必須在中得到的許可

Caution: Currently, all apps have the ability to read the external storage without a special permission. However, this will change in a future release. If your app needs to read the external storage (but not write

to it), then you will need to declare the <code></code> permission. To ensure that your app continues to work as

expected, you should declare this permission now, before the change takes effect.

注意:目前,所有應用程式沒有特别許可就可以讀取外部存儲。然而,這将在将來釋出的版本中改變。如果你的應用程式需要讀取外部存儲器(但不寫),那麼您将需要申報許可。確定應用程式繼續正常工作,你現在應該宣布這個許可,在更改生效。

However, if your app uses the <code></code> permission, then it implicitly has

permission to read the external storage as well.

You don’t need any permissions to save files on the internal storage. Your application always has permission to read and write files in its internal storage directory.

然而,如果您的應用程式使用許可,那麼它也可以讀取外部存儲器。

你不需要任何權限去儲存檔案到内部存儲。應用程式總是允許在其内部存儲目錄中讀取和寫入檔案。

在内部存儲儲存一個檔案

When saving a file to internal storage, you can acquire the appropriate directory as a

<code></code> by calling one of two methods:

當一個檔案儲存到内部存儲,您可以獲得适當的目錄作為一個檔案通過調用兩種方法之一:

<dl></dl>

<dt><code></code></dt>

<dd>Returns a <code></code> representing an internal directory for your app.為您的應用程式傳回一個代表内部檔案目錄。</dd>

<dd>Returns a <code></code> representing an internal directory for your app‘s temporary cache files. Be sure to delete each file once it is</dd>

no longer needed and implement a reasonable size limit for the amount of memory you use at any given time, such as 1MB. If the system begins running low on storage, it may delete your cache files without warning.

<dd>為應用程式的臨時緩存檔案傳回一個代表一個内部檔案目錄。一定要删除每個檔案一旦不再需要和實作一個合理的大小限制為您使用的記憶體數量在任何給定的時間,比如1 mb。如果系統開始運作低存儲,它可能毫無預警地删除緩存檔案。</dd>

To create a new file in one of these directories, you can use the

<code></code> constructor, passing the

<code></code> provided by one of the above methods that specifies your internal storage directory. For example:

其中的一個目錄中建立一個新檔案,您可以使用構造函數,通過上述方法之一提供的方法,指定您的内部存儲目錄。例如:

Alternatively, you can call <code></code> to get a

<code></code> that writes to a file in your internal directory. For example, here‘s how to write some text to a file:

或者,你可以在你的内部目錄中讓來獲得寫入一個檔案。例如,下面是如何編寫一些文本檔案:

Or, if you need to cache some files, you should instead use

<code></code>. For example, the following method extracts the file name from a

<code></code> and creates a file with that name in your app‘s internal cache directory:

或者,如果您需要緩存一些檔案,您應該使用。例如,下面的方法提取檔案的名字從一個URL并建立一個檔案,這個名字在你的應用程式的内部緩存目錄:

Note: Your app‘s internal storage directory is specified by your app‘s package name in a special location of the Android file system. Technically, another app can read your internal files if you set

the file mode to be readable. However, the other app would also need to know your app package name and file names. Other apps cannot browse your internal directories and do not have read or write access unless you explicitly set the files to be readable or

writable. So as long as you use <code></code> for your files on the internal storage, they are never accessible to other apps.

注意:您的應用程式的内部存儲目錄指定應用程式的包名稱在安卓系統檔案系統的一個特殊的位置。從技術上講,另一個應用程式可以讀取你的内部檔案如果你設定的檔案模式可讀。然而,其他應用程式也需要知道應用程式包名和檔案名。其他應用程式不能浏覽你的内部目錄和沒有讀或寫通路,除非你顯式地設定檔案讀取或寫入。是以隻要你使用内部存儲你的檔案,他們永遠不會通路其他應用程式。

在外部存儲儲存一個檔案

Because the external storage may be unavailable—such as when the user has mounted the storage to a PC or has removed the SD card that provides the external storage—you should always verify that the volume is available before accessing

it. You can query the external storage state by calling <code></code>. If the returned state is equal to

<code></code>, then you can read and write your files. For example, the following methods are useful to determine the storage availability:

因為外部存儲器可能不可獲得,例如當使用者安裝電腦的存儲或删除了SD卡,提供外部記憶體,您應該在通路之前驗證一下。你可以查詢通過調用外部存儲狀态.如果傳回的狀态等于,那麼您可以讀取和寫入檔案。例如,下面的方法是有用的來确定存儲可用性:

Although the external storage is modifiable by the user and other apps, there are two categories of files you might save here:

雖然外部存儲修改的由使用者和其他應用程式,這裡有兩個類别的檔案你可能節省:

<dt>Public files</dt>

<dd></dd>

Files that should be freely available to other apps and to the user. When the user uninstalls your app, these files should remain available to the user.  

For example, photos captured by your app or other downloaded files.

公共檔案

檔案應該免費提供給其他應用程式和使用者。當使用者解除安裝應用程式時,這些檔案應該保持使用者可用。

例如,照片拍攝到你的應用程式或其他下載下傳的檔案。

<dt>Private files</dt>

Files that rightfully belong to your app and should be deleted when the user uninstalls  your app. Although these files are technically accessible by the user and other apps because they  are on the external storage, they are

files that realistically don‘t provide value to the user  outside your app. When the user uninstalls your app, the system deletes  all files in your app‘s external private  directory.   

For example, additional resources downloaded by your app or temporary media files.

私密的檔案

合法檔案,屬于你的應用程式,應該删除當使用者解除安裝應用程式。盡管這些檔案在技術上可由使用者和其他應用程式,因為它們在外部存儲上,他們是實際的檔案不提供價值給使用者應用程式之外。當使用者解除安裝應用程式時,系統将删除所有檔案在您的應用程式的外部私人目錄。

例如,額外的資源下載下傳應用程式或臨時的媒體檔案。

If you want to save public files on the external storage, use the

<code></code> method to get a

<code></code> representing the appropriate directory on the external storage. The method takes an argument specifying the type of file you want to save so that they can

be logically organized with other public files, such as <code></code> or

<code></code>. For example:

如果你想節省公共外部存儲上的檔案,使用方法來得到一個外部存檔案代表相應的目錄。方法接受一個參數指定類型的檔案你想儲存,這樣他們可以在邏輯上組織與其他公共檔案,如或。例如:

If you want to save files that are private to your app, you can acquire the appropriate directory by calling

<code></code> and passing it a name indicating the type of directory you‘d like. Each directory created

this way is added to a parent directory that encapsulates all your app‘s external storage files, which the system deletes when the user uninstalls your app.

如果你想要私人的檔案儲存到您的應用程式,您可以通過調用方法獲得适當的目錄和通過它的名字顯示你想要的類型的目錄。這種方式建立的每個目錄添加到父目錄,封裝了應用程式的所有外部存儲檔案,系統删除當使用者解除安裝應用程式。

For example, here‘s a method you can use to create a directory for an individual photo album:

例如,這裡有一個方法可以用來建立一個目錄的個人相冊:

If none of the pre-defined sub-directory names suit your files, you can instead call

<code></code> and pass

<code>null</code>. This returns the root directory for your app‘s private directory on the external storage.

如果沒有一個預定義的子目錄名字适合你的檔案,你可以叫getExternalFilesDir()和傳遞null。這将傳回您的應用程式的根目錄的私人在外部存儲目錄。

Remember that <code></code> creates a directory inside a directory that

is deleted when the user uninstalls your app. If the files you‘re saving should remain available after the user uninstalls your app—such as when your app is a camera and the user will want to keep the photos—you should instead use

記住getExternalFilesDir()建立一個目錄在一個目錄,當使用者删除解除安裝應用程式。如果你儲存的檔案應該保持可用使用者解除安裝後app-such當你的應用程式是一個相機和使用者想保持photos-you應該使用getExternalStoragePublicDirectory()。

Regardless of whether you use <code></code>

for files that are shared or <code></code> for files that are private to your app, it‘s important that

you use directory names provided by API constants like <code></code>. These directory names ensure that the files are treated

properly by the system. For instance, files saved in <code></code> are categorized by the system media scanner as ringtones

instead of music.

無論您使用檔案共享或為私人的檔案到你的應用程式,重要的是你使用API提供的目錄名像.常量。這些目錄名確定檔案被正确的系統。例如,檔案儲存在由系統分類媒體掃描器的鈴聲,而不是音樂。

查詢空閑空間

If you know ahead of time how much data you‘re saving, you can find out whether sufficient space is available without causing an

<code></code> by calling

<code></code> or

<code></code>. These methods provide the current available space and the total space in the storage volume, respectively. This information

is also useful to avoid filling the storage volume above a certain threshold.

如果你知道提前多少資料你儲存,你可以找到足夠的空間是否可用而不會導緻一個IOException通過調用getFreeSpace()或getTotalSpace()。這些方法分别提供目前的可用空間和總空間存儲卷。這些資訊也是有用的,以避免填充存儲體積超過一定門檻值。

However, the system does not guarantee that you can write as many bytes as are indicated by

<code></code>.  If the number returned is a few MB more than the size of the data you want to save, or if the file system is less than 90% full,

then it‘s probably safe to proceed. Otherwise, you probably shouldn‘t write to storage.

然而,系統并不能保證你可以寫盡可能多的位元組被getFreeSpace表示()。如果傳回的數字是幾MB大小的超過你想儲存的資料,或者檔案系統還不到90%,那麼它可能是安全的。否則,你可能不能寫入存儲。

Note: You aren‘t required to check the amount of available space before you save your file. You can instead try writing the file right away, then catch an

<code></code> if one occurs. You may need to do this if you don‘t know exactly how much space you need. For example, if you change the file‘s encoding before

you save it by converting a PNG image to JPEG, you won‘t know the file‘s size beforehand.

注意:您不需要檢查的可用空間在你儲存你的檔案。你可以試着寫檔案,如果發生異常試着抓住一個。你可能需要這樣做,如果你不知道你需要多少空間。舉個例子,如果你改變檔案的編碼儲存之前将PNG圖像轉換成JPEG,而事先您不知道檔案的大小。

删除檔案

You should always delete files that you no longer need. The most straightforward way to delete a file is to have the opened file reference call

<code></code> on itself.

您可以删除不再需要的檔案。最直截了當的方式,删除一個檔案,打開檔案引用調用delete()。

If the file is saved on internal storage, you can also ask the

<code></code> to locate and delete a file by calling

<code></code>:

如果檔案是儲存在内部存儲,你還可以問上下文來定位和删除一個檔案通過調用deleteFile():

Note: When the user uninstalls your app, the Android system deletes the following:

注意:當使用者解除安裝應用程式,Android系統删除以下:

All files you saved on internal storage                   

你儲存在内部存儲的所有檔案

All files you saved on external storage using <code></code>.                  

所有使用方法儲存在外部存儲的檔案

However, you should manually delete all cached files created with

<code></code> on a regular basis and also regularly delete other files you no longer need.

然而,您應該手動删除所有通過方法建立的緩存檔案并定期删除你不再需要其他檔案。