Android——Android10的分區(qū)存儲(chǔ)(Scoped Storage)

分區(qū)存儲(chǔ)介紹

在Android10以前,只要程序獲得了READ_EXTERNAL_STORAGE權(quán)限,就可以隨意讀取外部的存儲(chǔ)公有目錄。只要程序獲得了WRITE_EXTERNAL_STORAGE權(quán)限,就可以隨意在寫(xiě)入外部存儲(chǔ)的公有目錄上新建文件或文件夾


Android Q之前,應(yīng)用存儲(chǔ)視圖

于是Google在Android10中提出了分區(qū)存儲(chǔ),意在限制程序?qū)ν獠看鎯?chǔ)中公有目錄的使用。
分區(qū)存儲(chǔ)對(duì)內(nèi)部存儲(chǔ)私有目錄和外部存儲(chǔ)私有目錄都沒(méi)有影響

Android Q之后應(yīng)用存儲(chǔ)視圖

簡(jiǎn)單來(lái)說(shuō)就是,在Android10中,

  • 對(duì)于私有目錄的讀寫(xiě)沒(méi)有變化,仍然可以使用File那一套,且不需要任何權(quán)限。
  • 對(duì)于公有目錄的讀寫(xiě),則必須使用MediaStore提供的API或是SAF(存儲(chǔ)訪問(wèn)框架)

在后續(xù)的Android11中,沒(méi)有了Android10中的兼容模式,不能使用File I/O來(lái)讀取App外置存儲(chǔ)的目錄

使用分區(qū)存儲(chǔ)的應(yīng)用對(duì)自己創(chuàng)建的文件始終擁有讀/寫(xiě)權(quán)限,無(wú)論文件是否位于應(yīng)用的私有目錄內(nèi),所以,如果應(yīng)用僅保存和訪問(wèn)自己創(chuàng)建的文件,則無(wú)需請(qǐng)求獲得READ_EXTERNAL_STORAGE或WRITE_EXTERNAL_STORAGE權(quán)限

如果要訪問(wèn)其他應(yīng)用創(chuàng)建的文件,則需要READ_EXTERNAL_STORAGE權(quán)限。并且仍然只能使用MediaStore提供的API或是SAF訪問(wèn)。
這里需要注意的是,MediaStore提供的API只能訪問(wèn)圖片、視頻、音頻,如果需要訪問(wèn)其它任意格式的文件,需要使用SAF,它會(huì)調(diào)用系統(tǒng)內(nèi)置的文件瀏覽器供用戶自主選擇文件

SAF是Android在 4.4中引入的一套存儲(chǔ)訪問(wèn)框架(Storage Access Framework),借助 SAF,用戶可輕松在其所有首選文檔存儲(chǔ)提供程序中瀏覽并打開(kāi)文檔、圖像及其他文件。用戶可通過(guò)易用的標(biāo)準(zhǔn)界面,以統(tǒng)一方式在所有應(yīng)用和提供程序中瀏覽文件,以及訪問(wèn)最近使用的文件。


SAF交互使用介紹

Android Q規(guī)定了App有兩種存儲(chǔ)空間模式視圖:Legacy View、Filtered View

  • Legacy View(兼容模式) 跟以前Android Q一樣,App訪問(wèn)Sdcard一樣
  • Filtered View(沙箱模式)
    App只能直接訪問(wèn)App-specific目錄文件,沒(méi)有權(quán)限訪問(wèn)App-specific外的文件。訪問(wèn)其他目錄,只能通過(guò)MediaStore、SAF、或者其他App提供ContentProvider訪問(wèn)

Scoped Storage的存儲(chǔ)空間

  • 公共目錄:Downloads、Documents、Pictures、DCIM、Movies、Music、Ringtones
    • 公共目錄的文件在App卸載后,不會(huì)刪除
    • 可以通過(guò)SAF、MediaStore接口訪問(wèn)
  • App-specific目錄
    • 對(duì)于Filtered View App,App-specific目錄只能自己直接訪問(wèn)
    • App卸載,數(shù)據(jù)會(huì)清除

運(yùn)行視圖

App運(yùn)行視圖

系統(tǒng)通過(guò)下列方式確定App的運(yùn)行模式:

  • App的TargetSDK>=Q,默認(rèn)為Filtered View
  • App的TargetSDK<Q,聲明了READ_EXTERNAL_STORAGE或者WRITE_EXTERNAL_STORAGE權(quán)限,默認(rèn)Legacy View
  • 應(yīng)用通過(guò)AndroidManifest.xml設(shè)置 requestLegacyExternalStorage
    • true:表示兼容模式Legacy View
    • false:表示沙箱模式 Filtered View

判斷當(dāng)前App的運(yùn)行模式

判斷當(dāng)前App運(yùn)行的是什么模式,可以通過(guò)Environment提供的API進(jìn)行判斷

Environment.isExternalStorageLegacy()

MediaStore的Uri定義

MediaStore提供了下列幾種類型的訪問(wèn)Uri,通過(guò)查找對(duì)應(yīng)Uri數(shù)據(jù),達(dá)到訪問(wèn)的目的。

獲取所有的Volume

我們還可以使用getContentUri獲取所有<volumeName>


image.png

Uri跟公共目錄關(guān)系

MediaProvider對(duì)于App存放到公共目錄文件,通過(guò)ContentResolver insert方法中Uri來(lái)確定


image.png

權(quán)限

MediaStroe通過(guò)不同Uri,為用戶提供了增、刪、改方法,權(quán)限對(duì)應(yīng)如下


image.png
  • 由上表可以看出沒(méi)在操作共享存儲(chǔ)空間時(shí),獲取的權(quán)限不同可以對(duì)應(yīng)不同的操作共享存儲(chǔ)空間的方式
    • WRITE_EXTERNAL_STORAGE:獲取這個(gè)權(quán)限,可以修改所有的app新建的文件,但是都需要授予權(quán)限
    • READ_EXTERNAL_STORAGE:獲取這個(gè)權(quán)限,可以讀取所有的app新建的文件,不能修改其他App新建的文件
    • 什么權(quán)限都不獲取的話,只能讀取、修改自己app新建的文件

操作共享存儲(chǔ)空間,讀寫(xiě)公共目錄

通過(guò)Media定義的URI

新建文件(通過(guò)ContentResolver的insert接口,使用不同的Uri選擇存儲(chǔ)到不同的目錄)


    /**
     * 通過(guò)SAF創(chuàng)建文件文件夾
     *
     * @param view
     */
    public void createSAF(View view) {
        Uri uri = MediaStore.Files.getContentUri("external");
        ContentResolver contentResolver = this.getContentResolver();
        //定義path
        String path = Environment.DIRECTORY_DOWNLOADS + "/wx";
        ContentValues contentValues = new ContentValues();
        contentValues.put(MediaStore.Downloads.RELATIVE_PATH, path);
        contentValues.put(MediaStore.Downloads.DISPLAY_NAME, "wxPic");
        contentValues.put(MediaStore.Downloads.TITLE, "it's title");
        Uri resultUri = contentResolver.insert(uri, contentValues);
        if (resultUri != null) {
            Toast.makeText(this, "創(chuàng)建文件夾成功", Toast.LENGTH_SHORT).show();
        }
    }

新建一張圖片

 /**
     * 插入一張圖片
     *
     * @param view
     */

    public void insertImage(View view) {
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.moto);
        String disPlayPicName = System.currentTimeMillis() + "123.jpg";
        String mimeType = "image/jpeg";
        ContentValues contentValues = new ContentValues();
        contentValues.put(MediaStore.Images.ImageColumns.DISPLAY_NAME, disPlayPicName);
        contentValues.put(MediaStore.Images.ImageColumns.MIME_TYPE, mimeType);
        contentValues.put(MediaStore.Images.ImageColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES + File.separator + "wx" + File.separator);
        imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
        OutputStream outputStream = null;
        try {
            outputStream = getContentResolver().openOutputStream(imageUri);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
            outputStream.close();
            FileDescriptor fileDescriptor = getContentResolver().openFileDescriptor(imageUri, "r").getFileDescriptor();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Toast.makeText(this, "添加圖片成功", Toast.LENGTH_SHORT).show();
    }

修改照片

   /**
     * 修改數(shù)據(jù)
     *
     * @param view
     */
    public void updateImage(View view) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(MediaStore.Images.ImageColumns.DISPLAY_NAME, "test.jpg");
        int update = getContentResolver().update(imageUri, contentValues, null, null);
        if (update > 0) {
            Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show();
        }
    }

查詢數(shù)據(jù)

  /**
     * 查詢數(shù)據(jù)
     *
     * @param view
     */
    public void query(View view) {
        //獲取URI
        Uri external = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        //創(chuàng)建selection
        String selection = MediaStore.Images.Media.DISPLAY_NAME + "=?";
        String[] arg = new String[]{"test.jpg"};
        Cursor cursor = getContentResolver().query(external, null, selection, arg, null);
        if (cursor != null && cursor.moveToFirst()) {
            Uri idUri = ContentUris.withAppendedId(external, cursor.getLong(0));
            Toast.makeText(this, "獲取成功" + idUri, Toast.LENGTH_SHORT).show();
            cursor.close();
        }
    }

刪除數(shù)據(jù)

 /**
     * 刪除圖片
     *
     * @param view
     */
    public void deleteImage(View view) {
        int delete = getContentResolver().delete(imageUri, null);
        if (delete > 0) {
            Toast.makeText(this, "刪除成功", Toast.LENGTH_SHORT).show();
        }
    }

關(guān)于RecoverableSecurityException異常
當(dāng)我們刪除其他應(yīng)用創(chuàng)建的資源時(shí)會(huì)報(bào)出RecoverableSecurityException異常,我們可以捕獲這個(gè)異常然后提示給與uri修改或刪除的權(quán)限

private fun deleteImage(imageUri: Uri, adapterPosition: Int) {
    var row = 0
    try {
        // Android 10+中,如果刪除的是其它應(yīng)用的Uri,則需要用戶授權(quán)
        // 會(huì)拋出RecoverableSecurityException異常
        row = contentResolver.delete(imageUri, null, null)
    } catch (securityException: SecurityException) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            val recoverableSecurityException =
                securityException as? RecoverableSecurityException
                    ?: throw securityException
            pendingDeleteImageUri = imageUri
            pendingDeletePosition = adapterPosition
            // 我們可以使用IntentSender向用戶發(fā)起授權(quán)
            requestRemovePermission(recoverableSecurityException.userAction.actionIntent.intentSender)
        } else {
            throw securityException
        }
    }

    if (row > 0) {
        Toast.makeText(this, "刪除成功", Toast.LENGTH_SHORT).show()
        pictureAdapter.deletePosition(adapterPosition)
    }
}

private fun requestRemovePermission(intentSender: IntentSender) {
    startIntentSenderForResult(intentSender, REQUEST_DELETE_PERMISSION, 
        null, 0, 0, 0, null)
}

private fun deletePendingImageUri(){
    pendingDeleteImageUri?.let {
        pendingDeleteImageUri = null
        deleteImage(it,pendingDeletePosition)
        pendingDeletePosition = -1
    }
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (resultCode == Activity.RESULT_OK &&
        requestCode == REQUEST_DELETE_PERMISSION
    ) {
        // 執(zhí)行之前的刪除邏輯
        deletePendingImageUri()
    }
}

如果想獲取Download文件夾下的某個(gè)非媒體文件怎么辦

例如PDF,PDF為非媒體類文件,因此我們不能通過(guò)MediaStore來(lái)獲取,對(duì)于這種其他類型的文件,一般使用SAF來(lái)讓用戶選擇

private fun selectPdfUseSAF() {
    val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
        type = "application/pdf"
        // 我們需要使用ContentResolver.openFileDescriptor讀取數(shù)據(jù)
        addCategory(Intent.CATEGORY_OPENABLE)
    }
    startActivityForResult(intent, REQUEST_OPEN_PDF)
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    when (requestCode) {
        REQUEST_OPEN_PDF -> {
            if (resultCode == Activity.RESULT_OK) {
                data?.data?.also { documentUri ->
                    val fileDescriptor =
                        contentResolver.openFileDescriptor(documentUri, "r") ?: return
                    // 現(xiàn)在,我們可以使用PdfRenderer等類通過(guò)fileDescriptor讀取pdf內(nèi)容
                    Toast.makeText(this, "pdf讀取成功", Toast.LENGTH_SHORT).show()
                }
            }
        }
    }
}

如何創(chuàng)建任意類型的文件

我們也推薦使用SAF讓用戶自己去創(chuàng)建,IntentAction為:ACTION_CREATE_DOCUMENT

訪問(wèn)App-specific目錄

訪問(wèn)app-specific分為兩種情況,一種是訪問(wèn)App自身App-specific目錄,第二是訪問(wèn)其他App目錄文件

App訪問(wèn)自身App-specific目錄

Android Q,App如果啟動(dòng)了Filtered View,那么只能直接訪問(wèn)自己目錄的文件:

  • Environment.getExternalStorageDirectory、getExternalStoragePublicDirectory這些接口在Android Q上廢棄,App是Filtered View,無(wú)法直接訪問(wèn)這個(gè)目錄。

  • 通過(guò)File(“/sdcard/”)訪問(wèn)App是Filtered View,無(wú)法直接訪問(wèn)這個(gè)目錄。

  • 獲取App-specific目錄

    • 獲取Media接口:getExternalMediaDirs
    • 獲取Cache接口:getExternalCacheDirs
    • 獲取Obb接口:getObbDirs
    • 獲取Data接口:getExternalFilesDirs

App訪問(wèn)App-sepecific目錄內(nèi)部的多媒體文件

  • App自身訪問(wèn),和App訪問(wèn)自身的App-soecific目錄一樣
  • 其他App訪問(wèn)
    • 默認(rèn)情況下,Media Scanner不會(huì)掃描App-specific里的多媒體文件,如果需要掃描通過(guò)MediaScannerConnection.scanFile添加到MediaProvider數(shù)據(jù)庫(kù)中,訪問(wèn)方式和訪問(wèn)共享存儲(chǔ)空間方式一樣
    • App通過(guò)創(chuàng)建ContentProvider共享出去

App訪問(wèn)其他App目錄文件

App是FilteredView,其他App無(wú)法直接訪問(wèn)當(dāng)前App私有目錄,需要通過(guò)以下方法:

通過(guò)SAF文件

  • App自定義DocumentsProvider
  • 訪問(wèn)App通過(guò)ACTION_OPEN_DOCUMENT啟動(dòng)SAF瀏覽

實(shí)現(xiàn)FileProvider(某些手機(jī)可能有問(wèn)題)

App自定義私有Provider

App訪問(wèn)不同目錄的權(quán)限總結(jié)
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 10(API 級(jí)別 29)引入了多項(xiàng)功能和行為變更,目的是更好地保護(hù)用戶的隱私權(quán)。其中最重要的變化...
    HurryYu_YZH閱讀 3,902評(píng)論 2 16
  • Android存儲(chǔ)目錄 內(nèi)部存儲(chǔ) getFilesDir - 應(yīng)用內(nèi)部存儲(chǔ) 放在data/data/package...
    stareme閱讀 3,237評(píng)論 0 3
  • Android 10(API 級(jí)別 29)引入了多項(xiàng)功能和行為變更,目的是更好地保護(hù)用戶的隱私權(quán)。其中最重要的變化...
    野火友燒不盡閱讀 8,458評(píng)論 0 4
  • Android Q 越來(lái)越近了,最近 Google 又發(fā)布了 Android Q Beta 的第五個(gè)版本,眼瞅著這...
    hljstardust閱讀 5,245評(píng)論 0 3
  • 16宿命:用概率思維提高你的勝算 以前的我是風(fēng)險(xiǎn)厭惡者,不喜歡去冒險(xiǎn),但是人生放棄了冒險(xiǎn),也就放棄了無(wú)數(shù)的可能。 ...
    yichen大刀閱讀 7,654評(píng)論 0 4

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