Android中關於內部儲存的一些重要函式

yangxi_001發表於2017-02-09
一、簡介
Android中,你也可以通過絕對路徑以JAVA傳統方式訪問內部儲存空間。但是以這種方式建立的檔案是對私有,建立它的應用程式對該檔案是可讀可寫,但是別的應用程式並不能直接訪問它。不是所有的內部儲存空間應用程式都可以訪問,預設情況下只能訪問“/data/data/你的應用程式的包名”這個路徑下的檔案。
Android中,你還可以使用Context物件的openFileOutput()openFileInput()來進行資料持久化儲存的這種方式,你的資料檔案將儲存在內部儲存空間的/data/data/你的應用程式的包名/files/目錄下,無法指定更深一級的目錄,而且預設是Context.MODE_PRIVATE模式,即別的應用程式不能訪問它。你可以使用openFileOutput()int mode引數來讓別的應用程式也能訪問你的檔案。
注意:儲存在/data/data/你的應用程式的包名目錄中檔案,會在解除安裝你的應用程式時被刪除掉。
 二、Context中關於內部儲存的重要函式 

public abstract File getCacheDir ()

Since: API Level 1

Returns the absolute path to the application specific cache directory on the filesystem. These files will be ones that get deleted first when the device runs low on storage. There is no guarantee when these files will be deleted. Note: you should not rely on the system deleting these files for you; you should always have a reasonable maximum, such as 1 MB, for the amount of space you consume with cache files, and prune those files when exceeding that space.

該目錄主要用於存放快取檔案,當系統的記憶體儲存空間緊張時,該目錄下的檔案會被刪除掉。關於這些檔案究竟會在儲存空間剩餘多少的情況,沒有嚴格的標準保障。
注意:你不應該依賴系統來清理這些快取檔案,你應該對這些快取檔案佔用的最大儲存空間設定個最大值,比如是1M,當實際佔用空間超過這個值時,你應該對這些快取檔案做相應的清理工作(prune)。
Returns
  • Returns the path of the directory holding application cache files.
See Also
  • openFileOutput(String, int)
  • getFileStreamPath(String)
  • getDir(String, int)
  • 示例1
  • import android.app.Activity;
  • import android.content.Context;
  • import android.os.Bundle;
  • import android.util.Log;
  • public class MainActivity extends Activity {
  • final static String TAG="robin";
  •     /** Called when the activity is first created. */
  •     @Override
  •     public void onCreate(Bundle savedInstanceState) {
  •         super.onCreate(savedInstanceState);
  •         setContentView(R.layout.main);
  •         Context context=this;
  •         String path=context.getCacheDir().getAbsolutePath();
  •         Log.i(TAG,"path:"+path);
  •     }
  • }

  • 執行結果
  • 10-01 14:57:52.296: I/robin(7835): path:/data/data/com.lenovo/cache

public abstract File getDir (String name, int mode)

Since: API Level 1

Retrieve, creating if needed, a new directory in which the application can place its own custom data files. You can use the returned File object to create and access files in this directory. Note that files created through a File object will only be accessible by your own application; you can only set the mode of the entire directory, not of individual files.

該函式主要用於得到一個資料夾的控制程式碼,並通過該控制程式碼建立和訪問外資料夾。
注意:引數int mode是指資料夾的訪問許可權而並不包括其子資料夾和檔案的訪問許可權
Parameters
name Name of the directory to retrieve. This is a directory that is created as part of your application data.
mode Operating mode. Use 0 or MODE_PRIVATE for the default operation, MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE to control permissions.
Returns
  • Returns a File object for the requested directory. The directory will have been created if it does not already exist.
See Also
  • openFileOutput(String, int)
  • 示例2
  •         File file=context.getDir("download", Context.MODE_PRIVATE);
  •         String path=file.getAbsolutePath();
  •         Log.i(TAG,"path:"+path);

  • 執行結果
  • 10-02 08:56:49.278: I/robin(12055): path:/data/data/com.lenovo/app_download

public abstract File getFileStreamPath (String name)

Since: API Level 1

Returns the absolute path on the filesystem where a file created with openFileOutput(String, int) is stored.

Parameters
name The name of the file for which you would like to get its path.
Returns
  • Returns an absolute path to the given file.
See Also
  • openFileOutput(String, int)
  • getFilesDir()
  • getDir(String, int)
  • 示例3
  •         File file=context.getFileStreamPath("download");
  •         String path=file.getAbsolutePath();
  •         Log.i(TAG,"path:"+path);

  • 執行結果
  • 10-02 09:17:55.913: I/robin(12507): path:/data/data/com.lenovo/files/download

public abstract File getFilesDir ()

Since: API Level 1

Returns the absolute path to the directory on the filesystem where files created with openFileOutput(String, int) are stored.

Returns
  • Returns the path of the directory holding application files.
示例4

        File file=context.getFilesDir();
        String path=file.getAbsolutePath();
        Log.i(TAG,"path:"+path);

執行結果
10-02 09:15:11.102: I/robin(12359): path:/data/data/com.lenovo/files

public abstract FileInputStream openFileInput (String name)

Since: API Level 1

Open a private file associated with this Context's application package for reading.

Parameters
name The name of the file to open; can not contain path separators.
Returns
  • FileInputStream Resulting input stream.

public abstract FileOutputStream openFileOutput (String name, int mode)

Since: API Level 1

Open a private file associated with this Context's application package for writing. Creates the file if it doesn't already exist.

Parameters
name The name of the file to open; can not contain path separators.
mode Operating mode. Use 0 or MODE_PRIVATE for the default operation, MODE_APPEND to append to an existing file, MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE to control permissions.
Returns
  • FileOutputStream Resulting output stream.
示例5

String FILENAME = "hello_file";
String string = "hello world!";
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_WORLD_READABLE);
fos.write(string.getBytes());
fos.close();

示例6

String FILENAME = "hello_file";
String string = "hello world!";
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_WORLD_READABLE);
fos.write(string.getBytes());
fos.close();

示例7

String FILENAME = "hello_file";
String string = "hello world!";
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_APPEND|Context.MODE_WORLD_READABLE;
fos.write(string.getBytes());
fos.close();

public abstract boolean deleteFile (String name)

Since: API Level 1

Delete the given private file associated with this Context's application package.

Parameters
name The name of the file to delete; can not contain path separators.
Returns
  • True if the file was successfully deleted; else false.

public abstract String[] fileList ()

Since: API Level 1

Returns an array of strings naming the private files associated with this Context's application package.

Returns
  • Array of strings naming the private files.
三、Environment關於記憶體部儲存的重要函式

public static File getDataDirectory ()

Since: API Level 1

Gets the Android data directory.

用File返回資料檔案的根目錄,返回的檔案的路徑為“/data”。該目錄下的檔案是隻讀。應用程式無法對該目錄下的檔案進行寫操作。

public static File getDownloadCacheDirectory ()

Since: API Level 1

Gets the Android Download/Cache content directory.

用File返回快取檔案的根目錄,返回的檔案的路徑為“/cache”。對於第三方應用程式。該目錄下的檔案是隻讀。第三方應用程式無法對該目錄下的檔案進行寫操作。

public static File getRootDirectory ()

Since: API Level 1

Gets the Android root directory.

用File返回Android系統檔案的根目錄,返回的檔案的路徑為“/system”。該目錄下的檔案是隻讀。應用程式無法對該目錄下的檔案進行寫操作。

結束

相關文章