Android內(nèi)部存儲(chǔ)與外部存儲(chǔ)的示例講解
什么是內(nèi)部存儲(chǔ)和外部存儲(chǔ)
1.內(nèi)部存儲(chǔ)與外部存儲(chǔ)的存儲(chǔ)介質(zhì):
內(nèi)部存儲(chǔ)的介質(zhì):RAM(內(nèi)存) + 內(nèi)部ROM
外部存儲(chǔ)的介質(zhì):外部ROM + SDCard(TS卡等等)。
2.內(nèi)部存儲(chǔ)與外部存儲(chǔ)的存儲(chǔ)特點(diǎn):
一般來說,以/data
開頭的是內(nèi)部存儲(chǔ)。且內(nèi)部存儲(chǔ)不需要任何權(quán)限。
例如:
- /data/data/< applicationId >/shared_prefs
- /data/data/< applicationId >/databases
- /data/data/< applicationId >/files // 通過
context.getFilesDir()
獲取該目錄 - /data/data/< applicationId >/cache //通過
context.getCacheDir()
獲取該目錄
內(nèi)部存儲(chǔ)需要關(guān)注的文件夾:
app文件夾
(未root無法打開):存放著所有app的apk文件夾
data文件夾
:內(nèi)部都是app的包名,存儲(chǔ)著應(yīng)用程序相關(guān)的數(shù)據(jù),例如 data/data/包名/(shared_prefs、database、files、cache
)
Android SDK提供了幾個(gè)常見的內(nèi)部存儲(chǔ)文件的權(quán)限
- Context.MODE_PRIVATE :私有方式存儲(chǔ),其他應(yīng)用無法訪問,覆蓋舊的同名文件
- Context.MODE_APPEND:私有方式存儲(chǔ),若有舊的同名文件,則在該文件上追加數(shù)據(jù)
一般來說,外部存儲(chǔ)會(huì)放在storage
文件夾下或者mnt
文件夾下。且需要安卓的權(quán)限。
例如:
私有外部存儲(chǔ)
- /storage/emulated/0/Android/data/< applicationId >/files/Music //
Context.getExternalFilesDir()
包含如Music等文件夾 - /storage/emulated/0/Android/data/< applicationId >/cache //
Context.getExternalCacheDir
外部緩存文件
以及共有外部存儲(chǔ)
/storage/emulated/0 Environment.getExternalStorageDirectory()
/storage/emulated/0/Pictures
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
外部存儲(chǔ)需要注意的文件夾即外部存儲(chǔ)的分類:
storage
中有一個(gè)sdcard
文件夾,sdcard下面可以分兩類存儲(chǔ):
外部共有存儲(chǔ)(共有目錄):里面常見的有Pictures、Download等文件夾.
外部私有存儲(chǔ)(私有目錄):系統(tǒng)中的數(shù)據(jù)。
內(nèi)部存儲(chǔ)與外部存儲(chǔ)的文件夾:
3.內(nèi)部存儲(chǔ)與外部存儲(chǔ),釋放內(nèi)存方面的總結(jié):
內(nèi)部存儲(chǔ):隨應(yīng)用卸載被刪除。外部存儲(chǔ):
1.公有目錄:存放一些下載的視頻文件等,比如還有movies,fictures,music等公有的一些文件目錄。
2.私有目錄:隨應(yīng)用卸載被刪除。
內(nèi)部存儲(chǔ)與外部存儲(chǔ)的代碼示例
內(nèi)部存儲(chǔ)
// 存 FileOutputStream fos = null; try { //第一個(gè)參數(shù):文件名 //第二個(gè)參數(shù):表示文件輸出的類型 這里選擇Context.MODE_PRIVATE每次生成相同的文件名,則覆蓋原有的文件 fos = openFileOutput('test', Context.MODE_PRIVATE); String nameAndPassword = username + "." + password; byte[] bytes = nameAndPassword.getBytes(); fos.write(bytes); } catch (Exception e) { e.printStackTrace(); } finally { if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } // 取 FileInputStream fis = null; try { fis = openFileInput(fileName); //fis.available() 判斷文件有多少個(gè)字節(jié) byte[] bytes = new byte[fis.available()]; while (fis.read(bytes) != -1) { String message = new String(bytes); String[] split = message.split("\\."); tv_message.setText("用戶名:" + split[0] + "\n" + "密碼:" + split[1]); } } catch (Exception e) { e.printStackTrace(); }
外部存儲(chǔ)
// 獲取外部存儲(chǔ)地址的位置并創(chuàng)建文件: new File(getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS) // 存 OutputStream outputStream = null; try { outputStream = new FileOutputStream(new File(getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), 'test2')); String nameAndPassword = username + "." + password; byte[] bytes = nameAndPassword.getBytes(); outputStream.write(bytes); } catch (Exception e) { e.printStackTrace(); } finally { if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } // 取 FileInputStream fis = null; try { //第一個(gè)參數(shù):文件目錄 第二個(gè)參數(shù):文件名 //getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)對(duì)應(yīng)的路徑為: // /storage/emulated/0/Android/data/com.example.customviewproject/files/Download File file = new File(getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), fileName); fis = new FileInputStream(file); //判斷當(dāng)前文件的字節(jié)個(gè)數(shù) byte[] bytes = new byte[fis.available()]; while (fis.read(bytes) != -1) { String message = new String(bytes); String[] split = message.split("\\."); tv_message.setText("用戶名:" + split[0] + "\n" + "密碼:" + split[1]); } } catch (Exception e) { e.printStackTrace(); } finally { if (fis != null) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } }
到此這篇關(guān)于Android內(nèi)部存儲(chǔ)與外部存儲(chǔ)的示例講解的文章就介紹到這了,更多相關(guān)Android內(nèi)部存儲(chǔ)與外部存儲(chǔ)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android 動(dòng)態(tài)顯示和隱藏狀態(tài)欄詳解及實(shí)例
這篇文章主要介紹了Android 動(dòng)態(tài)顯示和隱藏狀態(tài)欄的相關(guān)資料,需要的朋友可以參考下2017-06-06Android實(shí)現(xiàn)自動(dòng)播放圖片功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)自動(dòng)播放圖片功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06Android入門之計(jì)時(shí)器Chronometer的使用教程
Chronometer是一個(gè)簡(jiǎn)單的定時(shí)器,你可以給它一個(gè)開始時(shí)間,并以此定時(shí)。本文將利用個(gè)簡(jiǎn)單的示例為大家講解一下它的使用,感興趣的小伙伴可以嘗試一下2022-11-11Android入門之使用RecyclerView完美實(shí)現(xiàn)瀑布流界面詳解
網(wǎng)上充滿著不完善的基于RecyclerView的瀑布流實(shí)現(xiàn),要么根本是錯(cuò)的、要么就是只知其一不知其二。本文就來用RecyclerView完美實(shí)現(xiàn)瀑布流界面,希望大家有所幫助2023-02-02