Я новичок в Android студии. Итак, я получил исходный код от здесь . Это сработало для выбора файла. Однако я ожидаю получить только каталог. Пожалуйста, помогите мне, как отредактировать этот метод, чтобы добиться его
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>
Вот класс MainActivity
public class MainActivity extends AppCompatActivity {
private final static int READ_EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE = 13;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int permissionCheck = ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_EXTERNAL_STORAGE);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
READ_EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE);
} else {
addFileChooserFragment();
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String[] permissions,
@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == READ_EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
addFileChooserFragment();
}
}
}
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();
}
}
}
Эти зависимости дляиспользоваться
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'
}