zoukankan      html  css  js  c++  java
  • android 打开文件管理到指定目录,华为VCE-L22 Android 9 ,API 28

    今天试着调用系统的文件管理,结果蛋碎一地

    要打开的文件地址为 

    /storage/emulated/0/Android/data/net.topstarts/files/Download

    首先使用

    val intent = Intent(Intent.ACTION_VIEW)
    
    var uri: Uri? = null
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    uri = FileProvider.getUriForFile(
                        this, RetrofitClient.getPackageName(this) + ".topstart.provider", File(root)
                    )
                } else {
                    uri = Uri.fromFile(File(root))
                }
              intent.addCategory(Intent.CATEGORY_OPENABLE)
                intent.setDataAndType(uri, "file/*")
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
                intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
                startActivity(Intent.createChooser(intent, "选择文件"))
    

    结果:::

       系统提示  没有应用可执行此操作

    再切换成

    val intent = Intent(Intent.ACTION_GET_CONTENT)
    // 或者      val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
                var uri: Uri? = null
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    uri = FileProvider.getUriForFile(
                        this, RetrofitClient.getPackageName(this) + ".topstart.provider", File(root)
                    )
                } else {
                    uri = Uri.fromFile(File(root))
                }
    
               //intent.setDataAndType(uri, "*/*")
                intent.addCategory(Intent.CATEGORY_OPENABLE)
                intent.setDataAndType(uri, "file/*")
                //            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                //            intent.addCategory(Intent.CATEGORY_DEFAULT)
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
                intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
    
                startActivity(Intent.createChooser(intent, "选择文件"))
      
    

     结果:::

      intent.setDataAndType(uri, "file/*") 效果              intent.setDataAndType(uri, "*/*") 效果

      

                     

    并且,使用 

    val intent = Intent(Intent.ACTION_GET_CONTENT)

    // 或者      val intent = Intent(Intent.ACTION_OPEN_DOCUMENT) 都只是选择或者查看,
    其他操作都需要自己使用startActivityForResult(intent,reqestCode)来取得路径,实现相应操作,不是像调用地图一样直接使用
    --------------------------------------------------我是分隔线------------------------------------------------

    查询谷歌的文档
    https://developer.android.google.cn/training/data-storage/app-specific
    也未找到分享自己APP 外存地址的方法
    查询 stackoverflow android call file manage 也未找到相应的答案

    https://stackoverflow.com/questions/5298302/air-open-files-on-android-openwithdefaultapplication-alternative?r=SearchResults

    总结:::
    官方对每个APP文件的权限,在自己APP里可以随意调用,但对外授权则非常麻烦
    授权系统目录里的自己APP目录则只能查看

    未尝试 ContentProvidor 按理这种方式可以共享自己APP的私有目录数据
    未尝试 内存卡除APP私有目录,系统目录外  是否可以正常授权第三方



  • 相关阅读:
    JVM 垃圾回收器工作原理及使用实例介绍(转载自IBM),直接复制粘贴,需要原文戳链接
    装tomcat和nginx心得
    jms的俩种模式
    裸奔Spring(1)
    一个最小mybatis
    SpringBoot和数据库连接
    SpringBoot的基础Pom
    SpringBoot读取配置文件
    埃拉托斯特尼素数筛法
    hdu 1175 连连看
  • 原文地址:https://www.cnblogs.com/caosq/p/12972676.html
Copyright © 2011-2022 走看看