Android5.0+讀寫外部存儲(chǔ)卡

手上有部Android7.0的支持外置存儲(chǔ)卡的安卓手機(jī),想要在外置存儲(chǔ)卡根目錄下寫文件這么一個(gè)簡單功能折騰了一個(gè)下午,最后終于找到解決辦法了,這里分享給大家。

之所以從Android5.0之后安卓讀寫存儲(chǔ)變得很復(fù)雜的原因應(yīng)該還是不少App開發(fā)者濫用該功能導(dǎo)致文件夾混亂,并且當(dāng)App卸載的時(shí)候其文件夾仍然占用用戶存儲(chǔ)空間,所以Google只允許應(yīng)用在/Android/data/packagename下讀寫文件了。

本文會(huì)告訴大家如何隨意在外部存儲(chǔ)卡上讀寫文件,但是希望大家不要濫用該方法。
 /**
     * 獲取手機(jī)存儲(chǔ)路徑
     * /storage/emulated/0
     * 等同于 /sdcard
     */
    fun getInternalStoragePath(): String {
        return Environment.getExternalStorageDirectory().absolutePath
    }

    /**
     * 獲取外置存儲(chǔ)卡,包括U盤的路徑
     */
    fun getExternalStoragePaths(): ArrayList<String> {
        val paths = ArrayList<String>()

        try {
            val storageManager = BaseApplication.gInstance!!.getSystemService(AppCompatActivity.STORAGE_SERVICE) as StorageManager
            val getVolumeList = storageManager.javaClass.getDeclaredMethod("getVolumeList")
            val volumeList = getVolumeList.invoke(storageManager) as Array<Object>

            for (volume in volumeList) {
                val getPath = volume.`class`.getDeclaredMethod("getPath")
                val isRemovable = volume.`class`.getDeclaredMethod("isRemovable")
                val path = getPath.invoke(volume) as String
                val removable = isRemovable.invoke(volume) as Boolean
                if (removable) {
                    paths.add(path)
                }
            }

        } catch (e: Exception) {
            e.printStackTrace()
        }
        return paths
    }

/**
 * 調(diào)用系統(tǒng)文檔,對外置存儲(chǔ)卡路徑訪問授權(quán)
 **/
private val REQUEST_CODE_STORAGE_ACCESS = 100
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private fun triggerStorageAccessFramework() {
    val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
    startActivityForResult(intent, REQUEST_CODE_STORAGE_ACCESS)
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent) {
        if (requestCode == REQUEST_CODE_STORAGE_ACCESS) {
            if (resultCode == Activity.RESULT_OK) {
                val treeUri = resultData.data
                val pickedDir = DocumentFile.fromTreeUri(this, treeUri)
                var newFile = pickedDir.createDirectory("想創(chuàng)建的文件夾名字")

                if(PathHelper.getMyPath() != null) {
                    setResult(RESULT_OK)
                    finish()
                } else {
                    ToastHelper.show(R.string.error_create_root_path)
                }
            }
        }
    }

參考:
https://blog.csdn.net/mzm489321926/article/details/49329443
https://stackoverflow.com/questions/26744842/how-to-use-the-new-sd-card-access-api-presented-for-android-5-0-lollipop

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

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

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