[英]Supply default path for ACTION_OPEN_DOCUMENT
In my app I want to give users a way to pick a file from the app's data directory. 在我的应用程序中,我想为用户提供一种从应用程序的数据目录中选择文件的方法。 This is my code:
这是我的代码:
// use ACTION_OPEN_DOCUMENT because ACTION_GET_CONTENT will give us
// gallery and other stuff we don’t need
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
Uri uri = Uri.parse(getExternalFilesDir(null).getAbsolutePath());
Log.d(TAG, "Browsing " + uri.toString());
intent.setDataAndType(uri, "*/*");
// show the entire internal storage tree
intent.putExtra("android.content.extra.SHOW_ADVANCED", true);
startActivityForResult(intent, 42);
The logcat shows me that the URI that I am setting is file:///sdcard/Android/data/my.app/files
, but the file picker UI defaults to the shared storage root ( /sdcard
). Logcat向我显示我正在设置的URI是
file:///sdcard/Android/data/my.app/files
,但是文件选择器UI默认为共享存储根( /sdcard
)。
The following code works (requires API 26+ as per the documentation, the intent is available from the API as DocumentsContract.EXTRA_INITIAL_URI
): 以下代码有效(根据文档要求使用API 26+,该意图可从该文档作为
DocumentsContract.EXTRA_INITIAL_URI
):
// works only with this intent, at the expense of gallery etc. appearing
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
// apparently we need a valid content URI
Uri uri = Uri.parse("content://com.android.externalstorage.documents/document/primary%3AAndroid%2Fdata%2Fmy.app%2Ffiles");
intent.putExtra("android.provider.extra.INITIAL_URI", uri);
Log.d(TAG, "Browsing " + uri.toString());
intent.setType("*/*");
// show the entire internal storage tree
intent.putExtra("android.content.extra.SHOW_ADVANCED", true);
startActivityForResult(intent, 42);
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, uri);
However, ACTION_GET_CONTENT
causes all kinds of providers to appear, such as Gallery and Music, when all I need is the local file system (and in fact just the app's private subtree). 但是,当我需要的只是本地文件系统(实际上只是应用程序的私有子树)时,
ACTION_GET_CONTENT
会导致出现各种类型的提供程序,例如Gallery和Music。 If I change the intent to ACTION_OPEN_DOCUMENT
, the URI I supply is ignored. 如果我将意图更改为
ACTION_OPEN_DOCUMENT
,那么我提供的URI将被忽略。
How can I get the file picker UI to start in a directory of my choice, with only a minimal choice of content providers? 我如何才能使文件选择器UI在您选择的目录中启动,而内容提供商的选择却很少?
Edit: Testing this on Anbox, which I just realize is only at API 25—in fact, I need a way that works on APIs as low as 24. 编辑:在Anbox上进行测试,我才意识到这仅在API 25上进行-实际上,我需要一种可在24级以下的API上工作的方法。
There may not be a universally viable solution, but the following has worked on some builds (though not on others): 可能没有一个普遍可行的解决方案,但是以下内容已在某些构建中起作用(尽管不适用于其他构建):
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
Uri uri = Uri.parse("content://com.android.externalstorage.documents/document/primary%3AAndroid%2Fdata%2Fmy.app%2Ffiles");
intent.setData(uri);
intent.setType("*/*");
intent.putExtra("android.provider.extra.INITIAL_URI", uri);
intent.putExtra("android.content.extra.SHOW_ADVANCED", true);
startActivityForResult(intent, 42);
uri
must be a content
URI for the com.android.externalstorage.documents
provider. uri
必须是com.android.externalstorage.documents
提供程序的content
URI。 The URI path is /document/primary%3A
, followed by the filesystem path to the folder to start in. The path must be relative to the shared storage root (ie drop the leading /sdcard/
or equivalent on the device and make sure the result does not start with a slash) and escaped. URI路径为
/document/primary%3A
,后跟要开始的文件夹的文件系统路径。该路径必须相对于共享存储根(即,在设备上删除开头的/sdcard/
或等效文件,并确保结果不以斜杠开头)并转义。
The call to Intent#setData()
does not help in setting the default location (unlike with some third-party file managers) but prevents unwanted storage providers (such as Gallery and Music) from being displayed. 对
Intent#setData()
的调用无助于设置默认位置(与某些第三方文件管理器不同),但可以防止显示不需要的存储提供程序(例如Gallery和Music)。
The android.provider.extra.INITIAL_URI
extra sets the initial URI, but this may not work prior to API 26 (although it does work on some flavors of Android). android.provider.extra.INITIAL_URI
额外设置了初始URI,但这可能在API 26之前不起作用(尽管它在某些Android版本上也有效)。
The android.content.extra.SHOW_ADVANCED
extra causes device storage to be available as a provider (otherwise, depending on the flavor of Android, it may require the user to select it or not be available at all). android.content.extra.SHOW_ADVANCED
额外导致设备存储可作为提供程序使用(否则,取决于Android的风格,它可能要求用户选择它或根本不使用它)。
Again, still not a perfect solution but the closest I managed to get. 同样,仍然不是一个完美的解决方案,但我设法获得了最接近的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.