Android10 分區(qū)存儲(chǔ)的適配規(guī)則
存儲(chǔ)權(quán)限
Android Q 仍然使用 READ_EXTRNAL_STORAGE 和 WRITE_EXTRNAL_STORAGE 作為存儲(chǔ)相關(guān)運(yùn)行時(shí)權(quán)限 但現(xiàn)在即使
獲取了這些權(quán)限,訪問外部存儲(chǔ)也受到了限制,只能訪問自身目錄下的文件和公共體內(nèi)的文件
內(nèi)部存儲(chǔ) 外部存儲(chǔ)
內(nèi)部存儲(chǔ) | 外部存儲(chǔ) | 備注 | |
---|---|---|---|
英文名稱 | Internal storage | External storage | |
版本變更 | 不變 | 4.4之前,外部存儲(chǔ)僅僅代表SD卡之類的移動(dòng)存儲(chǔ)設(shè)備,4.4之后包括內(nèi)置的外部存儲(chǔ)和SD卡(部分手機(jī)沒有提供SD卡的卡槽,就只有內(nèi)置的外部存儲(chǔ)) | |
查看方法 | 用模擬器+adb shell 進(jìn)入 或者是Android Studio Devices File Explorer | 一般的文檔管理App 都能看 | 用 su root 命令給模擬器加權(quán)限 |
組成成分 | System/:存放系用數(shù)據(jù) data/: 存放應(yīng)用相關(guān)數(shù)據(jù) vendor/:用于存放廠商客制化的數(shù)據(jù)等 | 私有存儲(chǔ)區(qū):android/ 文件夾下,是應(yīng)用的私有存儲(chǔ)區(qū)域 公共存儲(chǔ)區(qū)域:Movie、Download、 DCIM、 Picture、Documents、Ringtones、Music、 Alarms | |
存儲(chǔ)內(nèi)容 | db share preference files cache 等 | 開發(fā)者自己需要存儲(chǔ)的數(shù)據(jù) 如視頻文件、音頻文件、或者一些表格 日志文件 | 內(nèi)部存儲(chǔ)小而且寶貴我們基本上不要操作它,需要存儲(chǔ)的都存儲(chǔ)在外部存儲(chǔ) |
獲取路徑方法 | Environment.getDataDirectory() Context.getFileDir() | Environment.getExternalStorageDirectory() (traget>=30,已廢棄) Context.getExternalFilesDir() | 基本上Context的方法獲取的都是應(yīng)用的私有存儲(chǔ)路徑 E nvironment的方法獲取的都是根目錄 |
應(yīng)用卸載時(shí) | 私有路徑下的文件全部刪除 即:data/user/0/packageName/ | 私有路徑下的文件全部刪除 即:android/data/packageName/ 公共存儲(chǔ)區(qū)域不變動(dòng) |
適配
- 獲取外部存儲(chǔ) 文件夾
// 如果當(dāng)前目錄下沒有 fileDirName 文件夾會(huì)自動(dòng)創(chuàng)建 val file:File = context.getExternalFileDir("fileDirName") // fileDirName 文件夾名稱 // /storage/emulated/0/Android/data/packageName/files/fileDirName
- 創(chuàng)建外部存儲(chǔ) 文件
val appFileDirName = applicationContext.getExternalFilesDir("fileDirName")?.absolutePath val newFile = File(appFileDirName, "temp.txt") val fileWriter = FileWriter(newFile) fileWriter.write("test information") fileWriter.flush() fileWriter.close()
- 創(chuàng)建外部存儲(chǔ)公共目錄下的文件路徑
/** * @param fileName 文件名稱 * @param relativePath 包含某個(gè)媒體下的子路徑 */ fun insertFileIntroMediaStore( context: Context, fileName: String, relativePath: String ): Uri? { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) { return null } val contentResolver = context.contentResolver val values = ContentValues() values.put(MediaStore.Downloads.DISPLAY_NAME, fileName) values.put(MediaStore.Downloads.MIME_TYPE, "text/plain") values.put(MediaStore.Downloads.RELATIVE_PATH, relativePath) //驗(yàn)證存儲(chǔ)空間的可用性 //由于外部存儲(chǔ)空間位于用戶可能能夠移除的物理卷上,因此在嘗試從外部存儲(chǔ)空間讀取應(yīng)用專 //屬數(shù)據(jù)或?qū)?yīng)用專屬數(shù)據(jù)寫入外部存儲(chǔ)空間之前,請(qǐng)驗(yàn)證該卷是否可訪問。 //您可以通過調(diào)用 Environment.getExternalStorageState() 查詢?cè)摼淼臓顟B(tài)。如果返回的狀態(tài)為 MEDIA_MOUNTED,那么您就可以在外部存儲(chǔ)空間中讀取和寫入應(yīng)用專屬文件。如果返回的狀態(tài)為 MEDIA_MOUNTED_READ_ONLY,您只能讀取這些文件。 val externalStorageState = Environment.getExternalStorageState() return if (externalStorageState.equals(Environment.MEDIA_MOUNTED)) { contentResolver.insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, values) } else { contentResolver.insert(MediaStore.Downloads.INTERNAL_CONTENT_URI, values) } } /** * @param context 上下文 * @param insertUri 存儲(chǔ)Uri * @param inputStream 文件輸出流 */ fun saveFile(context: Context, insertUri: Uri?, inputStream: InputStream?) { insertUri ?: return inputStream ?: return val resolver = context.contentResolver val out = resolver.openOutputStream(insertUri) var read: Int val buffer = ByteArray(1024) while (inputStream.read(buffer).also { read = it } != -1) { out?.write(buffer) } inputStream.close() out?.flush() out?.close() } /** * @param context 上下文 * @param insertUri 存儲(chǔ)Uri * @param sourceFile 資源文件 */ fun saveFile(context: Context, insertUri: Uri?, sourceFile: File?) { insertUri ?: return sourceFile ?: return val inputStream = FileInputStream(sourceFile) val resolver = context.contentResolver val out = resolver.openOutputStream(insertUri) var read: Int val buffer = ByteArray(1024) while (inputStream.read(buffer).also { read = it } != -1) { out?.write(buffer) } inputStream.close() out?.flush() out?.close() }
- 讀取外部存儲(chǔ) 公共目錄文件
/** * 獲取文件輸出流 by uri * @param context 上下文 * @param uri 文件路徑 */ fun getInputStreamByUri(context: Context, uri: Uri?): InputStream? { uri ?: return null val openFileDescriptor = context.contentResolver.openFileDescriptor(uri, "r") return FileInputStream(openFileDescriptor?.fileDescriptor) }
以上就是Android10 分區(qū)存儲(chǔ)的使用總結(jié)的詳細(xì)內(nèi)容,更多關(guān)于Android10 分區(qū)存儲(chǔ)的使用的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
flutter 微信聊天輸入框功能實(shí)現(xiàn)
這篇文章主要介紹了flutter 微信聊天輸入框功能實(shí)現(xiàn),本文通過實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03android通過自定義toast實(shí)現(xiàn)懸浮通知效果的示例代碼
這篇文章主要介紹了android通過自定義toast實(shí)現(xiàn)懸浮通知效果,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-10-10Android與H5交互產(chǎn)生Script Error踩坑解決
這篇文章主要為大家介紹了Android與H5交互產(chǎn)生Script Error問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08Android 自定義控件實(shí)現(xiàn)顯示文字的功能
這篇文章主要介紹了Android 自定義控件實(shí)現(xiàn)顯示文字的功能的相關(guān)資料,需要的朋友可以參考下2016-11-11