我搜索了很多,找到了内容类型vnd.android.document/directory。
此外,这包括针对目标 API 24 及更高版本的其他解决方案。它显示了打开它的两个应用程序:OpenIntent 文件管理器和内置文件管理器。
这是研究。而不是一个完整的答案。如果您发现内置文件管理器的参数应该是什么,请在此处告诉我们。
File saveLocations 即指向 DCIM 文件夹。
// open a location with the file exporer
// see https://stackoverflow.com/a/26651827/1320237
// see https://stackoverflow.com/a/38858040
// see https://stackoverflow.com/a/8727354
Uri saveLocationUri = FileProvider.getUriForFile(this, this.getPackageName() + ".provider", saveLocation);
final Intent openSaveLocationIntent = new Intent(Intent.ACTION_VIEW);
openSaveLocationIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
openSaveLocationIntent.setDataAndType(saveLocationUri, "vnd.android.document/directory");
// see http://www.openintents.org/action/android-intent-action-view/file-directory
openSaveLocationIntent.putExtra("org.openintents.extra.ABSOLUTE_PATH", saveLocation.toString());
if (openSaveLocationIntent.resolveActivityInfo(getPackageManager(), 0) != null) {
startActivity(openSaveLocationIntent);
} else {
// if you reach this place, it means there is no any file
// explorer app installed on your device
}
看来,Android 24+ 有可能。
有一个意图过滤器来打开目录:
<activity
android:name=".files.FilesActivity"
android:documentLaunchMode="intoExisting"
android:theme="@style/DocumentsTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="vnd.android.document/root" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="vnd.android.document/directory" />
</intent-filter>
</activity>
这里有更多代码:
https://github.com/derp-caf/packages_apps_DocumentsUI/blob/ab8e8638c87cd5f7fe1005800520e739b3a48cd5/src/com/android/documentsui/ScopedAccessActivity.java#L114
我不确定如何正确设置参数。
// Sets args that will be retrieve on onCreate()
final Bundle args = new Bundle();
args.putString(EXTRA_FILE, file.getAbsolutePath());
args.putString(EXTRA_VOLUME_LABEL, volumeLabel);
args.putString(EXTRA_VOLUME_UUID, isPrimary ? null : storageVolume.getUuid());
args.putString(EXTRA_APP_LABEL, appLabel);
args.putBoolean(EXTRA_IS_ROOT, isRoot);
args.putBoolean(EXTRA_IS_PRIMARY, isPrimary);
我尝试输入一些额外信息,但它仍会在根文件夹中打开活动。
// from https://github.com/derp-caf/packages_apps_DocumentsUI/blob/ab8e8638c87cd5f7fe1005800520e739b3a48cd5/src/com/android/documentsui/ScopedAccessActivity.java#L114
openSaveLocationIntent.putExtra("com.android.documentsui.FILE", saveLocation.toString());
openSaveLocationIntent.putExtra("com.android.documentsui.IS_ROOT", false); // is the root folder?
openSaveLocationIntent.putExtra("com.android.documentsui.IS_PRIMARY", true); // is the primary volume? SD-Card should be false
openSaveLocationIntent.putExtra("com.android.documentsui.APP_LABEL", getTitle());