0

我是 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'
 }
4

0 回答 0