Android studio 如何获取目录
我是安卓工作室的新手。所以我从中获得了源代码。它适用于文件选择。 但是,我希望只获取目录。请帮助我如何编辑这个方法来实现它Android studio 如何获取目录,android-studio,directory,android-afilechooser,Android Studio,Directory,Android Afilechooser,我是安卓工作室的新手。所以我从中获得了源代码。它适用于文件选择。 但是,我希望只获取目录。请帮助我如何编辑这个方法来实现它 private void addFileChooserFragment() { FileChooser.Builder builder = new FileChooser.Builder(FileChooser.ChooserType.FILE_CHOOSER, new FileChooser.ChooserListener() {
private void addFileChooserFragment() {
FileChooser.Builder builder = new FileChooser.Builder(FileChooser.ChooserType.FILE_CHOOSER,
new FileChooser.ChooserListener() {
@Override
public void onSelect(String path) {
Toast.makeText(MainActivity.this, path, Toast.LENGTH_SHORT).show();
}
});
try {
getSupportFragmentManager().beginTransaction()
.add(R.id.file_chooser_fragment_container_framelayout, builder.build())
.commit();
} catch (ExternalStorageNotAvailableException e) {
Toast.makeText(this, "There is no external storage available on this device.",
Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
这里是activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/file_chooser_fragment_container_framelayout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
这些依赖项将被使用
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'ir.sohreco.androidfilechooser:android-file-chooser:1.3'
}
但是我希望只获取目录。
。奇怪。我读到:FileChooser
。在代码中的什么地方可以找到文件的路径;使用FileChooser.Builder。。。。你可以看到。我想得到目录。该程序获取文件的特定路径,并通过Toast.makeText(MainActivity.This,path,Toast.LENGTH_SHORT.show()显示;我看不到任何代码可以获取文件的路径并对其进行处理。请检查路径或记录它。我想这样做。请阅读文档:Android文件选择器是一个简单且可自定义的文件/目录选择器片段,您可以在应用程序中使用它,让您的用户根据您的需要选择文件或目录。
。特别是以下文字:可自定义文件/目录选择器
您可以看到。我想获取目录
?我在哪里可以看到?相反,我清楚地看到,您想要的文件类型是FileChooser.ChooserType.file\u CHOOSER
。
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'ir.sohreco.androidfilechooser:android-file-chooser:1.3'
}