打开指定路径

该方法只能打开外部存储下的路径,即打开手机文件管理器,显示的文件夹。必须要在这个文件夹(/storage/emulated/0)下的路径才能打开。
因国内机型可能对原生andriod进行修改,不能保证所有机型都能打开。

    /**
     * 打开指定文件夹(只能打开外部存储中的文件夹)
     *
     * @param absolutePath 文件夹路径 例如:/storage/emulated/0
     */
    @RequiresApi(api = Build.VERSION_CODES.O)
    public void openFolder(@NonNull Context activity, @NonNull String absolutePath) {
        String path;
        String externalStorageDir = "/storage/emulated/0";
        if (absolutePath.startsWith(externalStorageDir)) {
            path = absolutePath
                    .substring(externalStorageDir.length())
                    .replace("/", "%2f");
        } else {
            path = absolutePath.replace("/", "%2f");
        }
        Uri uri = Uri.parse("content://com.android.externalstorage.documents/document/primary:"
                + path);
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("*/*");
        intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, uri);
        activity.startActivity(intent);
    }

文件夹打开后,显示空白没有文件,可能是没有文件访问的权限。可参考这篇博客添加权限

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐