android-檔案儲存體

來源:互聯網
上載者:User

android 檔案儲存體分為:internal storage(內部儲存)和 External storage(外部儲存例如sdcard)

internal storage:

1.內部儲存一直存在

2.預設的只能被你自己的應用訪問(當然通過設定 MODE_PRIVATE讓其他應用訪問而且其他應用要知道你的包名因為file儲存的地址在/data/data/packname)下面。

3.當你刪除此應用,這些檔案也會被刪除

4.訪問內部儲存不用manifest裡面加許可權

5.一般apk安裝的在內部儲存中當然也可以通過在manifest中設定android:installLocation設定apk安裝的目錄

6.內部目錄分為filesDir和cacheDir 可以通過方法getFileDir()、getCacheDir()去擷取他的地址

7.在內部儲存放置的檔案不用的要進行刪除,如果一直不刪除超出了系統分給你的大小,系統會在沒有警告的自動刪除你的儲存的檔案

8.內部檔案的寫入

String filename = "myfile";String string = "Hello world!";FileOutputStream outputStream;try {  outputStream = openFileOutput(filename, Context.MODE_PRIVATE);  outputStream.write(string.getBytes());  outputStream.close();} catch (Exception e) {  e.printStackTrace();}
9.內部檔案臨時檔案的寫入


public File getTempFile(Context context, String url) {    File file;    try {        String fileName = Uri.parse(url).getLastPathSegment();        file = File.createTempFile(fileName, null, context.getCacheDir());    catch (IOException e) {        // Error while creating file    }    return file;}

External Storage

1.外部檔案不一定一直都在。

2.外部檔案可以通過其他方式讀取

3.當你刪除應用是檔案還是會存在

4.如果你向外部檔案進行寫入需要在manifest中加入許可權:uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE“

如果你要讀取尾外部檔案到現在位置的版本是不需要加如許可權的但是為了保證向後相容最好在讀檔案時也加入許可權:uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"

備忘:android:installLocation

Although apps are installed onto the internal storage by default, you can specify theandroid:installLocation 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 App Install Location.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.