Android 8.0 打开Download目录和Android/data目录

记录一个打开Download目录和Android/data目录的方法

读写文件目录首先在AndroidManifest.xml 文件中声明权限

<!-- 读取文件权限 -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<!-- 写入文件权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>


<!-- Android 13(targetSdk 33) 文件读写权限 -->
<!-- Required only if your app needs to access images or photos
     that other apps created. -->
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />

<!-- Required only if your app needs to access videos
         that other apps created. -->
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />

<!-- Required only if your app needs to access audio files
         that other apps created. -->
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />

1、打开Download目录

需要注意子目录 '/' 需要转义为 '%2F'

    /**
     * 打开下载目录 Android 6.0 以上 打开子目录路径 ‘/’ 需要转义为 ‘%2F’
     *
     * @param context Context
     */
    public static void openDownloadDirectory(Context context) {
        // 打开Download目录Uri
        Uri uri = Uri.parse("content://com.android.externalstorage.documents/document/primary:Download");
        // 打开Download目录下子目录, 打开子目录路径 ‘/’需要转义为‘%2F’, 否则无法打开。
        // Uri uri = Uri.parse("content://com.android.externalstorage.documents/document/primary:Download%2FBrowser");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
            intent.addCategory(Intent.CATEGORY_OPENABLE);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setType("*/*");
            intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, uri);
            context.startActivity(intent);
        }
    }

2、打开Android/data 目录

需要注意primary后面的 ':'需要转义为'%3A',子目录路径 '/' 需要转义为 '%2F'

    /**
     * 打开 sdcard卡 Android/data/packageName 目录,需要判断目录是否存在
     * 注意 primary后面的 ’:‘ 需要转义为 ’%3A‘ , 子目录的 ’/‘ 需要转义为 ’%2F,
     *
     * @param context Context
     */
    public static void openAndroidDataDirectory(Activity context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // uri 打开 Android/data目录, 注意 primary后面的 ’:‘ 需要转义为 ’%3A‘ , 子目录的 ’/‘ 需要转义为 ’%2F‘
            Uri uri = Uri.parse("content://com.android.externalstorage.documents/document/primary%3AAndroid%2Fdata");
            // uri 打开应用的外部缓存目录,需要判断一下目录是否存在
//            String cachePath = "%2F" + context.getPackageName() + "%2F" + "cache";
//            Uri uri = Uri.parse("content://com.android.externalstorage.documents/document/primary%3AAndroid%2Fdata" + cachePath);
            Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
            intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, uri);
            //flag看实际业务需要可再补充
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION
                    | Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivityForResult(intent, 500);
        }
    }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值