「Android」使用SAF打开指定目录

配置读写权限

AndroidManifest

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

使用SAF打开指定目录

使用SAF打开微信下载目录Demo

    void openWeChatDownloadDirectory() {
        Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
        Uri weChatUri = Uri.parse("content://com.android.externalstorage.documents/document/primary%3AAndroid%2Fdata%2Fcom.tencent.mm%2FMicroMsg%2FDownload");
        intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, weChatUri);
        intent.setType("*/*");
        String[] mimeTypes = {
                "application/msword",
                "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
                "application/vnd.ms-powerpoint",
                "application/vnd.openxmlformats-officedocument.presentationml.presentation",
                "application/vnd.ms-excel",
                "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                "application/pdf"
        };
        intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
        startActivityForResult(intent, REQUEST_CODE);
    }

EXTRA_INITIAL_URI

    /**
     * Sets the desired initial location visible to user when file chooser is shown.
     *
     * <p>Applicable to {@link Intent} with actions:
     * <ul>
     *      <li>{@link Intent#ACTION_OPEN_DOCUMENT}</li>
     *      <li>{@link Intent#ACTION_CREATE_DOCUMENT}</li>
     *      <li>{@link Intent#ACTION_OPEN_DOCUMENT_TREE}</li>
     * </ul>
     *
     * <p>Location should specify a document URI or a tree URI with document ID. If
     * this URI identifies a non-directory, document navigator will attempt to use the parent
     * of the document as the initial location.
     *
     * <p>The initial location is system specific if this extra is missing or document navigator
     * failed to locate the desired initial location.
     */
    public static final String EXTRA_INITIAL_URI = "android.provider.extra.INITIAL_URI";

ACTION_OPEN_DOCUMENT

    /**
     * Activity Action: Allow the user to select and return one or more existing
     * documents. When invoked, the system will display the various
     * {@link DocumentsProvider} instances installed on the device, letting the
     * user interactively navigate through them. These documents include local
     * media, such as photos and video, and documents provided by installed
     * cloud storage providers.
     * <p>
     * Each document is represented as a {@code content://} URI backed by a
     * {@link DocumentsProvider}, which can be opened as a stream with
     * {@link ContentResolver#openFileDescriptor(Uri, String)}, or queried for
     * {@link android.provider.DocumentsContract.Document} metadata.
     * <p>
     * All selected documents are returned to the calling application with
     * persistable read and write permission grants. If you want to maintain
     * access to the documents across device reboots, you need to explicitly
     * take the persistable permissions using
     * {@link ContentResolver#takePersistableUriPermission(Uri, int)}.
     * <p>
     * Callers must indicate the acceptable document MIME types through
     * {@link #setType(String)}. For example, to select photos, use
     * {@code image/*}. If multiple disjoint MIME types are acceptable, define
     * them in {@link #EXTRA_MIME_TYPES} and {@link #setType(String)} to
     * {@literal *}/*.
     * <p>
     * If the caller can handle multiple returned items (the user performing
     * multiple selection), then you can specify {@link #EXTRA_ALLOW_MULTIPLE}
     * to indicate this.
     * <p>
     * Callers must include {@link #CATEGORY_OPENABLE} in the Intent to obtain
     * URIs that can be opened with
     * {@link ContentResolver#openFileDescriptor(Uri, String)}.
     * <p>
     * Callers can set a document URI through
     * {@link DocumentsContract#EXTRA_INITIAL_URI} to indicate the initial
     * location of documents navigator. System will do its best to launch the
     * navigator in the specified document if it's a folder, or the folder that
     * contains the specified document if not.
     * <p>
     * Output: The URI of the item that was picked, returned in
     * {@link #getData()}. This must be a {@code content://} URI so that any
     * receiver can access it. If multiple documents were selected, they are
     * returned in {@link #getClipData()}.
     *
     * @see DocumentsContract
     * @see #ACTION_OPEN_DOCUMENT_TREE
     * @see #ACTION_CREATE_DOCUMENT
     * @see #FLAG_GRANT_PERSISTABLE_URI_PERMISSION
     */
    @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
    public static final String ACTION_OPEN_DOCUMENT = "android.intent.action.OPEN_DOCUMENT";

常见App的目录

微信

微信7.0.15及以上版本:
图片及视频:文件管理 > 浏览 > 我的手机 > Pictures > WeiXin
文档:文件管理 > 浏览 > 我的手机(或存储卡) > Android > data > com.tencent.mm > MicroMsg > Download

QQ

QQ8.2.8及以上版本:
图片:文件管理 > 浏览 > 我的手机(或存储卡) > tencent > QQ_Images
视频:文件管理 > 浏览 > 我的手机(或存储卡) > Movies
文件:文件管理 > 浏览 > 我的手机(或存储卡) > Android > data > com.tencent.mobileqq > Tencent > QQfile_recv

QQ浏览器

文件管理 > 浏览 > 我的手机 > QQBrowser

自带浏览器

文件管理 > 浏览 > 我的手机(或存储卡) > Download,进入Download目录以及Download/browser下查找下载的文件

百度云盘

文件管理 > 浏览 > 我的手机(或存储卡) > BaiduNetdisk

WPS

文件管理 > 浏览 > 我的手机 > Android > data > cn.wps.moffice_eng目录下查找下载的文件

更多内容请参考:https://consumer.huawei.com/c...

山庄的铁匠
15 声望11 粉丝