1. 程式人生 > >Android中關於內部儲存的一些重要函式

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

也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!

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

public abstract File getCacheDir ()

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
  • 示例1
  • import android.app.Activity;
  • import android.content.Context;
  • import android.os.Bundle;
  • import android.util.Log;
  • publicclassMainActivityextendsActivity{
  • finalstaticString TAG="robin";
  • /** Called when the activity is first created. */
  • @Override
  • publicvoid 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 FilegetDir(String name, int mode)

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
nameName of the directory to retrieve. This is a directory that is created as part of your application data.
Returns
  • Returns a File object for the requested directory. The directory will have been created if it does not already exist.
See Also
  • 示例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 FilegetFileStreamPath

Returns the absolute path on the filesystem where a file created with  is stored.

Parameters
nameThe name of the file for which you would like to get its path.
Returns
  • Returns an absolute path to the given file.
See Also
  • 示例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 ()

Returns the absolute path to the directory on the filesystem where files created with  are stored.

Returns
  • Returns the path of the directory holding application files.
示例4File 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

openFileInput 

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

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

openFileOutput (String name, int mode)

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

Parameters
nameThe name of the file to open; can not contain path separators.
mode
Returns
  • FileOutputStream Resulting output stream.
示例5String FILENAME ="hello_file";Stringstring="hello world!";FileOutputStream fos = openFileOutput(FILENAME,Context.MODE_WORLD_READABLE);fos.write(string.getBytes());fos.close();示例6String FILENAME ="hello_file";Stringstring="hello world!";FileOutputStream fos = openFileOutput(FILENAME,Context.MODE_WORLD_READABLE);fos.write(string.getBytes());fos.close();示例7String FILENAME ="hello_file";Stringstring="hello world!";FileOutputStream fos = openFileOutput(FILENAME,Context.MODE_APPEND|Context.MODE_WORLD_READABLE;fos.write(string.getBytes());fos.close();

public abstract boolean deleteFile 

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

Parameters
nameThe 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 ()

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 ()

Gets the Android data directory.

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

public static File getDownloadCacheDirectory ()

Gets the Android Download/Cache content directory.

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

public static File getRootDirectory ()

Gets the Android root directory.

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

結束            這裡寫圖片描述 你好! 這是你第一次使用 **Markdown編輯器** 所展示的歡迎頁。如果你想學習如何使用Markdown編輯器, 可以仔細閱讀這篇文章,瞭解一下Markdown的基本語法知識。

新的改變

我們對Markdown編輯器進行了一些功能拓展與語法支援,除了標準的Markdown編輯器功能,我們增加了如下幾點新功能,幫助你用它寫部落格:

  1. 全新的介面設計 ,將會帶來全新的寫作體驗;
  2. 在創作中心設定你喜愛的程式碼高亮樣式,Markdown 將程式碼片顯示選擇的高亮樣式 進行展示;
  3. 增加了 圖片拖拽 功能,你可以將本地的圖片直接拖拽到編輯區域直接展示;
  4. 全新的 KaTeX數學公式 語法;
  5. 增加了支援甘特圖的mermaid語法1 功能;
  6. 增加了 多螢幕編輯 Markdown文章功能;
  7. 增加了 焦點寫作模式、預覽模式、簡潔寫作模式、左右區域同步滾輪設定 等功能,功能按鈕位於編輯區域與預覽區域中間;
  8. 增加了 檢查列表 功能。

功能快捷鍵

撤銷:Ctrl/Command + Z 重做:Ctrl/Command + Y 加粗:Ctrl/Command + B 斜體:Ctrl/Command + I 標題:Ctrl/Command + Shift + H 無序列表:Ctrl/Command + Shift + U 有序列表:Ctrl/Command + Shift + O 檢查列表:Ctrl/Command + Shift + C 插入程式碼:Ctrl/Command + Shift + K 插入連結:Ctrl/Command + Shift + L 插入圖片:Ctrl/Command + Shift + G

合理的建立標題,有助於目錄的生成

直接輸入1次#,並按下space後,將生成1級標題。 輸入2次#,並按下space後,將生成2級標題。 以此類推,我們支援6級標題。有助於使用TOC語法後生成一個完美的目錄。

如何改變文字的樣式

強調文字 強調文字

加粗文字 加粗文字

標記文字

刪除文字

引用文字

H2O is是液體。

210 運算結果是 1024.

插入連結與圖片

連結: link.

圖片: Alt

帶尺寸的圖片: Alt

當然,我們為了讓使用者更加便捷,我們增加了圖片拖拽功能。

如何插入一段漂亮的程式碼片

部落格設定頁面,選擇一款你喜歡的程式碼片高亮樣式,下面展示同樣高亮的 程式碼片.

// An highlighted block var foo = 'bar'; 

生成一個適合你的列表

  • 專案
    • 專案
      • 專案
  1. 專案1
  2. 專案2
  3. 專案3
  • 計劃任務
  • 完成任務

建立一個表格

一個簡單的表格是這麼建立的:

專案 Value
電腦 $1600
手機 $12
導管 $1

設定內容居中、居左、居右

使用:---------:居中 使用:----------居左 使用----------:居右

第一列 第二列 第三列
第一列文字居中 第二列文字居右 第三列文字居左

SmartyPants

SmartyPants將ASCII標點字元轉換為“智慧”印刷標點HTML實體。例如:

TYPE ASCII HTML
Single backticks 'Isn't this fun?' ‘Isn’t this fun?’
Quotes "Isn't this fun?" “Isn’t this fun?”
Dashes -- is en-dash, --- is em-dash – is en-dash, — is em-dash

建立一個自定義列表

Markdown
Text-to-HTML conversion tool
Authors
John
Luke

如何建立一個註腳

一個具有註腳的文字。2

註釋也是必不可少的

Markdown將文字轉換為 HTML

KaTeX數學公式

您可以使用渲染LaTeX數學表示式 KaTeX:

Gamma公式展示 Γ(n)=(n1)!nN\Gamma(n) = (n-1)!\quad\forall n\in\mathbb N 是通過尤拉積分

Γ(z)=0tz1etdt . \Gamma(z) = \int_0^\infty t^{z-1}e^{-t}dt\,.

你可以找到更多關於的資訊 LaTeX 數學表示式here.

新的甘特圖功能,豐富你的文章

gantt
        dateFormat  YYYY-MM-DD
        title Adding GANTT diagram functionality to mermaid
        section 現有任務
        已完成               :done,    des1, 2014-01-06,2014-01-08
        進行中               :active,  des2, 2014-01-09, 3d
        計劃一               :         des3, after des2, 5d
        計劃二               :         des4, after des3, 5d
  • 關於 甘特圖 語法,參考 這兒,

UML 圖表

可以使用UML圖表進行渲染。 Mermaid. 例如下面產生的一個序列圖::

張三李四王五你好!李四, 最近怎麼樣?你最近怎麼樣,王五?我很好,謝謝!我很好,謝謝!李四想了很長時間,文字太長了不適合放在一行.打量著王五...很好... 王五, 你怎麼樣?張三李四王五

這將產生一個流程圖。:

連結長方形圓角長方形菱形
  • 關於 Mermaid 語法,參考 這兒,

FLowchart流程圖

我們依舊會支援flowchart的流程圖:

  • 關於 Flowchart流程圖 語法,參考 這兒.

匯出與匯入

匯出

如果你想嘗試使用此編輯器, 你可以在此篇文章任意編輯。當你完成了一篇文章的寫作, 在上方工具欄找到 文章匯出 ,生成一個.md檔案或者.html檔案進行本地儲存。

匯入

如果你想載入一篇你寫過的.md檔案或者.html檔案,在上方工具欄可以選擇匯入功能進行對應副檔名的檔案匯入, 繼續你的創作。