无法使用Storage Access Framework(SAF)设置文档导航器的初始位置 [英] Cannot set the initial location of documents navigator using Storage Access Framework (SAF)

查看:187
本文介绍了无法使用Storage Access Framework(SAF)设置文档导航器的初始位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文档说:调用者可以通过 EXTRA_INITIAL_URI 设置文档URI,以指示文档导航器的初始位置."

The documentation says "Callers can set a document URI through EXTRA_INITIAL_URI to indicate the initial location of documents navigator."

但是它不会在这里解析 EXTRA_INITIAL_URI .我尝试设置为此给出的常量,如注释所示( EXTRA_INITIAL_URI ="android.provider.extra.INITIAL_URI" ),但这不起作用.如何解决?

But it won't resolve EXTRA_INITIAL_URI here. I tried setting the constant given for this as shown in the comment (EXTRA_INITIAL_URI = "android.provider.extra.INITIAL_URI"), but that does not work. How do I get it to resolve?

package com.ship.saftwo;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.io.File;

public class MainActivity extends AppCompatActivity{
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {navigateToAppDirectory();}});}

    private static final int READ_REQUEST_CODE = 42;
    // adding this does not help: public static final String EXTRA_INITIAL_URI = "android.provider.extra.INITIAL_URI";

    public void navigateToAppDirectory() {
        Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
        Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath() + File.separator + getResources().getString(R.string.app_name) + File.separator);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType("*/*");
        intent.putExtra(Intent.EXTRA_INITIAL_URI, uri);
        //error message: Cannot resolve symbol 'EXTRA_INITIAL_URI'
        startActivityForResult(intent, READ_REQUEST_CODE);
    }
}

推荐答案

来自 DocumentsContract.EXTRA_INITIAL_URI :

位置应指定文档URI或带有文档ID的树URI.如果此URI标识了非目录,则文档导航器将尝试使用文档的父级作为初始位置.

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.

据我所知

  • 您不能使用任意URI(例如, Uri.fromFile ,它需要源自DocumentsProvider
  • 您不能在API 25及以下版本中指定起始位置
  • You cannot specify a starting location using an arbitrary URI, e.g. Uri.fromFile, it needs to originate from a DocumentsProvider
  • You cannot specify a starting location on API 25 and below

假设从 Intent.ACTION_OPEN_DOCUMENT_TREE :

DocumentFile file = DocumentFile.fromTreeUri(context, uri);
intent.putExtra(EXTRA_INITIAL_URI, file.getUri());

这篇关于无法使用Storage Access Framework(SAF)设置文档导航器的初始位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆