Android10 分區(qū)存儲(chǔ)的適配規(guī)則
Android Q 仍然使用 READ_EXTRNAL_STORAGE 和 WRITE_EXTRNAL_STORAGE 作為存儲(chǔ)相關(guān)運(yùn)行時(shí)權(quán)限 但現(xiàn)在即使
獲取了這些權(quán)限,訪問(wèn)外部存儲(chǔ)也受到了限制,只能訪問(wèn)自身目錄下的文件和公共體內(nèi)的文件
內(nèi)部存儲(chǔ) 外部存儲(chǔ)內(nèi)部存儲(chǔ) 外部存儲(chǔ) 備注 英文名稱 Internal storage External storage版本變更 不變 4.4之前,外部存儲(chǔ)僅僅代表SD卡之類(lèi)的移動(dòng)存儲(chǔ)設(shè)備,4.4之后包括內(nèi)置的外部存儲(chǔ)和SD卡(部分手機(jī)沒(méi)有提供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 等 開(kāi)發(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)前目錄下沒(méi)有 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.contentResolverval 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ù)寫(xiě)入外部存儲(chǔ)空間之前,請(qǐng)驗(yàn)證該卷是否可訪問(wèn)。 //您可以通過(guò)調(diào)用 Environment.getExternalStorageState() 查詢?cè)摼淼臓顟B(tài)。如果返回的狀態(tài)為 MEDIA_MOUNTED,那么您就可以在外部存儲(chǔ)空間中讀取和寫(xiě)入應(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 ?: returninputStream ?: returnval resolver = context.contentResolverval out = resolver.openOutputStream(insertUri)var read: Intval 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 ?: returnsourceFile ?: returnval inputStream = FileInputStream(sourceFile)val resolver = context.contentResolverval out = resolver.openOutputStream(insertUri)var read: Intval 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 nullval 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)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. moment轉(zhuǎn)化時(shí)間戳出現(xiàn)Invalid Date的問(wèn)題及解決2. python爬蟲(chóng)實(shí)戰(zhàn)之制作屬于自己的一個(gè)IP代理模塊3. Python中內(nèi)建模塊collections如何使用4. 關(guān)于WPF WriteableBitmap類(lèi)直接操作像素點(diǎn)的問(wèn)題5. UDDI FAQs6. 使用JSP技術(shù)實(shí)現(xiàn)一個(gè)簡(jiǎn)單的在線測(cè)試系統(tǒng)的實(shí)例詳解7. Python中matplotlib如何改變畫(huà)圖的字體8. python實(shí)現(xiàn)坦克大戰(zhàn)9. 開(kāi)發(fā)效率翻倍的Web API使用技巧10. 跟我學(xué)XSL(一)第1/5頁(yè)
