美文网首页程序员
Android5.0+读写外部存储卡

Android5.0+读写外部存储卡

作者: H3c | 来源:发表于2018-04-02 16:17 被阅读66次
    手上有部Android7.0的支持外置存储卡的安卓手机,想要在外置存储卡根目录下写文件这么一个简单功能折腾了一个下午,最后终于找到解决办法了,这里分享给大家。
    
    之所以从Android5.0之后安卓读写存储变得很复杂的原因应该还是不少App开发者滥用该功能导致文件夹混乱,并且当App卸载的时候其文件夹仍然占用用户存储空间,所以Google只允许应用在/Android/data/packagename下读写文件了。
    
    本文会告诉大家如何随意在外部存储卡上读写文件,但是希望大家不要滥用该方法。
    
     /**
         * 获取手机存储路径
         * /storage/emulated/0
         * 等同于 /sdcard
         */
        fun getInternalStoragePath(): String {
            return Environment.getExternalStorageDirectory().absolutePath
        }
    
        /**
         * 获取外置存储卡,包括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
        }
    
    /**
     * 调用系统文档,对外置存储卡路径访问授权
     **/
    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("想创建的文件夹名字")
    
                    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

    相关文章

      网友评论

        本文标题:Android5.0+读写外部存储卡

        本文链接:https://www.haomeiwen.com/subject/tqdxhftx.html