AndroidQ(10)分區(qū)存儲完美適配

前言

最近時間在做AndroidQ的適配,截止到今天AndroidQ分區(qū)存儲適配完成,期間出現(xiàn)很多坑,目前網(wǎng)上的帖子大部分都是概述變更內(nèi)容,接下來的幾篇帖子都是對分區(qū)存儲實際經(jīng)驗代碼總結,填坑經(jīng)驗,特此記錄一下,也為大家提供幫助。

相關系列文章

本篇主要是對AndroidQ(10)分區(qū)存儲適配具體實現(xiàn)

要點:
  • Android Q文件存儲機制修改成了沙盒模式
  • APP只能訪問自己目錄下的文件和公共媒體文件
  • 對于AndroidQ以下,還是使用老的文件存儲方式

這里需要注意:在適配AndroidQ的時候還要兼容Q系統(tǒng)版本以下的,使用SDK_VERSION區(qū)分

背景

存儲權限

Android Q仍然使用READ_EXTERNAL_STORAGEWRITE_EXTERNAL_STORAGE作為存儲相關運行時權限,但現(xiàn)在即使獲取了這些權限,訪問外部存儲也受到了限制,只能訪問自身目錄下的文件和公共內(nèi)體文件。

header 1 無權限 READ_EXTERNAL
Audio 可讀寫APP自己創(chuàng)建的文件,但不可直接使用路徑訪問 可以讀其他APP創(chuàng)建的媒體類文件,刪改操作需要用戶授權
Image 可讀寫APP自己創(chuàng)建的文件,但不可直接使用路徑訪問 可以讀其他APP創(chuàng)建的媒體類文件,刪改操作需要用戶授權
File 可讀寫APP自己創(chuàng)建的文件,但不可直接使用路徑訪問 不可讀寫其他APP創(chuàng)建的非媒體類文件
Downloads 可讀寫APP自己創(chuàng)建的文件,但不可直接使用路徑訪問 不可讀寫其他APP創(chuàng)建的非媒體類文件

外部存儲結構劃分

  • 公有目錄:DownloadsDocuments、Pictures 、DCIM、Movies、Music、Ringtones

    地址:/storage/emulated/0/Downloads(Pictures)等

    公有目錄下的文件不會跟隨APP卸載而刪除。

  • APP私有目錄

    地址:/storage/emulated/0/Android/data/包名/files

    私有目錄存放app的私有文件,會隨著App的卸載而刪除。

適配指導

AndroidQ中使用ContentResolver進行文件的增刪改查

1、獲取(創(chuàng)建)自身目錄下的文件夾

  • 獲取及創(chuàng)建,如果手機中沒有對應的文件夾,則系統(tǒng)會自動生成
//在自身目錄下創(chuàng)建apk文件夾
File apkFile = context.getExternalFilesDir("apk");

2、創(chuàng)建自身目錄下的文件

  • 生成需要下載的路徑,通過輸入輸出流讀取寫入
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)建并獲取公共目錄下的文件路徑

通過MediaStore.insert寫入

//這里的fileName指文件名,不包含路徑
//relativePath 包含某個媒體下的子路徑
 private static Uri insertFileIntoMediaStore (String fileName, String fileType,String relativePath) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
       return null;
    }
    ContentResolver resolver = context.getContentResolver();
    //設置文件參數(shù)到ContentValues中
    ContentValues values = new ContentValues();
    //設置文件名
    values.put(MediaStore.Downloads.DISPLAY_NAME, fileName);
    //設置文件描述,這里以文件名為例子
    values.put(MediaStore.Downloads.DESCRIPTION, fileName);
    //設置文件類型
    values.put(MediaStore.Downloads.MIME_TYPE,"application/vnd.android.package-archive");
    //注意RELATIVE_PATH需要targetVersion=29
    //故該方法只可在Android10的手機上執(zhí)行
    values.put(MediaStore.Downloads.RELATIVE_PATH, relativePath);
    //EXTERNAL_CONTENT_URI代表外部存儲器
    Uri external = MediaStore.Downloads.EXTERNAL_CONTENT_URI;
    //insertUri表示文件保存的uri路徑
    Uri insertUri  = resolver.insert(external, values);
    return insertUri;
}

4、公共目錄下的指定文件夾下創(chuàng)建文件

結合上面代碼,我們主要是在公共目錄下創(chuàng)建文件或文件夾拿到本地路徑uri,不同的Uri,可以保存到不同的公共目錄中。接下來使用輸入輸出流就可以寫入文件

  下面代碼僅是以文件復制存儲舉例,`sourcePath`表示原文件地址,復制文件到新的目錄; 文件下載直接下載本地,無需文件復制。

重點:AndroidQ中不支持file://類型訪問文件,只能通過uri方式訪問

private static void saveFile(Context context, Uri insertUri){
    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()) { // 文件存在時
           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、使用MediaStore讀取公共目錄下的文件

通過ContentResolver openFileDescriptor接口,選擇對應的打開方式。
例如”r”表示讀,”w”表示寫,返回ParcelFileDescriptor類型的文件描述符。

 private static Bitmap readUriToBitmap (Context context, Uri uri) {
    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();
            //轉換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刪除文件

public static void deleteFile (Context context, Uri fileUri) {
    context.getContentResolver().delete(fileUri, null, null);
}

后續(xù)對AndroidQ存儲針對具體功能做介紹,歡迎關注~

大佬點個贊再走唄~
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容