AndroidQ(10)分區(qū)存儲(chǔ)完美適配方法
前言
最近時(shí)間在做AndroidQ的適配,截止到今天AndroidQ分區(qū)存儲(chǔ)適配完成,期間出現(xiàn)很多坑,目前網(wǎng)上的帖子大部分都是概述變更內(nèi)容,接下來(lái)的幾篇帖子都是對(duì)分區(qū)存儲(chǔ)實(shí)際經(jīng)驗(yàn)代碼總結(jié),填坑經(jīng)驗(yàn),特此記錄一下,也為大家提供幫助。
本篇主要是對(duì)AndroidQ(10)分區(qū)存儲(chǔ)適配具體實(shí)現(xiàn)
- 要點(diǎn):
- Android Q文件存儲(chǔ)機(jī)制修改成了沙盒模式
- APP只能訪問(wèn)自己目錄下的文件和公共媒體文件
- 對(duì)于AndroidQ以下,還是使用老的文件存儲(chǔ)方式
這里需要注意:在適配AndroidQ的時(shí)候還要兼容Q系統(tǒng)版本以下的,使用SDK_VERSION區(qū)分
背景
存儲(chǔ)權(quán)限
Android Q仍然使用READ_EXTERNAL_STORAGE和WRITE_EXTERNAL_STORAGE作為存儲(chǔ)相關(guān)運(yùn)行時(shí)權(quán)限,但現(xiàn)在即使獲取了這些權(quán)限,訪問(wèn)外部存儲(chǔ)也受到了限制,只能訪問(wèn)自身目錄下的文件和公共內(nèi)體文件。
外部存儲(chǔ)結(jié)構(gòu)劃分
公有目錄:Downloads、Documents、Pictures 、DCIM、Movies、Music、Ringtones等
地址:/storage/emulated/0/Downloads(Pictures)等
公有目錄下的文件不會(huì)跟隨APP卸載而刪除。
APP私有目錄
地址:/storage/emulated/0/Android/data/包名/files
私有目錄存放app的私有文件,會(huì)隨著App的卸載而刪除。
適配指導(dǎo)
AndroidQ中使用ContentResolver進(jìn)行文件的增刪改查
1、獲取(創(chuàng)建)自身目錄下的文件夾
獲取及創(chuàng)建,如果手機(jī)中沒(méi)有對(duì)應(yīng)的文件夾,則系統(tǒng)會(huì)自動(dòng)生成
//在自身目錄下創(chuàng)建apk文件夾 File apkFile = context.getExternalFilesDir("apk");
2、創(chuàng)建自身目錄下的文件
生成需要下載的路徑,通過(guò)輸入輸出流讀取寫入
String apkFilePath = context.getExternalFilesDir("apk").getAbsolutePath(); File newFile = new File(apkFilePath + File.separator + "temp.apk"); OutputStream os = null; try { os = new FileOutputStream(newFile); if (os != null) { os.write("file is created".getBytes(StandardCharsets.UTF_8)); os.flush(); } } catch (IOException e) { } finally { try { if (os != null) { os.close(); } } catch (IOException e1) { } }
3、創(chuàng)建公共目錄下的文件夾
通過(guò)MediaStore.insert寫入
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) { return null; } ContentResolver resolver = context.getContentResolver(); ContentValues values = new ContentValues(); values.put(MediaStore.Downloads.DISPLAY_NAME, fileName); values.put(MediaStore.Downloads.DESCRIPTION, fileName); //設(shè)置文件類型 values.put(MediaStore.Downloads.MIME_TYPE, "application/vnd.android.package-archive"); //注意MediaStore.Downloads.RELATIVE_PATH需要targetVersion=29, //故該方法只可在Android10的手機(jī)上執(zhí)行 values.put(MediaStore.Downloads.RELATIVE_PATH, "Download" + File.separator + "apk"); Uri external = MediaStore.Downloads.EXTERNAL_CONTENT_URI; Uri insertUri = resolver.insert(external, values); return insertUri;
4、公共目錄下的指定文件夾下創(chuàng)建文件
結(jié)合上面代碼,我們主要是在公共目錄下創(chuàng)建文件或文件夾拿到本地路徑uri,不同的Uri,可以保存到不同的公共目錄中。接下來(lái)使用輸入輸出流就可以寫入文件
重點(diǎn):AndroidQ中不支持file://類型訪問(wèn)文件,只能通過(guò)uri方式訪問(wèn)
ContentResolver resolver = context.getContentResolver(); Uri insertUri = resolver.insert(external, values); if(insertUri == null) { return; } String mFilePath = insertUri.toString(); InputStream is = null; OutputStream os = null; try { os = resolver.openOutputStream(insertUri); if(os == null){ return; } int read; File sourceFile = new File(sourcePath); if (sourceFile.exists()) { // 文件存在時(shí) is = new FileInputStream(sourceFile); // 讀入原文件 byte[] buffer = new byte[1024]; while ((read = is.read(buffer)) != -1) { os.write(buffer, 0, read); } } } catch (Exception e) { e.printStackTrace(); }finally { try { if (is != null) { is.close(); } if (os != null) { os.close(); } } catch (IOException e) { e.printStackTrace(); } }
5、通過(guò)MediaStore讀取公共目錄下的文件
ParcelFileDescriptor parcelFileDescriptor = null; FileDescriptor fileDescriptor = null; Bitmap tagBitmap = null; try { parcelFileDescriptor = context.getContentResolver().openFileDescriptor(uri, "r"); if (parcelFileDescriptor != null && parcelFileDescriptor.getFileDescriptor() != null) { fileDescriptor = parcelFileDescriptor.getFileDescriptor(); //轉(zhuǎn)換uri為bitmap類型 tagBitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (parcelFileDescriptor != null) { parcelFileDescriptor.close(); } } catch (IOException e) { } }
6、使用MediaStore刪除文件
context.getContentResolver().delete(fileUri, null, null);
7、APP通過(guò)MediaStore訪問(wèn)文件所需要的權(quán)限
header 1 | 無(wú)權(quán)限 | READ_EXTERNAL |
---|---|---|
Audio | 可讀寫APP自己創(chuàng)建的文件,但不可直接使用路徑訪問(wèn) | 可以讀其他APP創(chuàng)建的媒體類文件,刪改操作需要用戶授權(quán) |
Image | 可讀寫APP自己創(chuàng)建的文件,但不可直接使用路徑訪問(wèn) | 可以讀其他APP創(chuàng)建的媒體類文件,刪改操作需要用戶授權(quán) |
File | 可讀寫APP自己創(chuàng)建的文件,但不可直接使用路徑訪問(wèn) | 不可讀寫其他APP創(chuàng)建的非媒體類文件 |
Downloads | 可讀寫APP自己創(chuàng)建的文件,但不可直接使用路徑訪問(wèn) | 不可讀寫其他APP創(chuàng)建的非媒體類文件 |
后續(xù)對(duì)AndroidQ存儲(chǔ)針對(duì)具體功能做介紹,歡迎關(guān)注~
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:Android實(shí)現(xiàn)選項(xiàng)菜單子菜單
欄 目:Android
下一篇:android自定義view實(shí)現(xiàn)鐘表效果
本文標(biāo)題:AndroidQ(10)分區(qū)存儲(chǔ)完美適配方法
本文地址:http://mengdiqiu.com.cn/a1/Android/8983.html
您可能感興趣的文章


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方
- 01-10android實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
- 01-10android實(shí)現(xiàn)記住用戶名和密碼以及自動(dòng)
- 01-10C++自定義API函數(shù)實(shí)現(xiàn)大數(shù)相乘算法
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代
- 01-10android實(shí)現(xiàn)指紋識(shí)別功能
- 01-10如何給Flutter界面切換實(shí)現(xiàn)點(diǎn)特效
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條
- 01-10Emoji表情在Android JNI中的兼容性問(wèn)題詳
隨機(jī)閱讀
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10delphi制作wav文件的方法
- 04-02jquery與jsp,用jquery
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改