0

I'm new in Android studio. So I got source code from here. It worked for file choosing. enter image description here However I expect to get directory only. Please help me how to edit this method to achieve it

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();
    }
}

Here is 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>

Here is MainActivity Class

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();
    }
}
}

These dependencies to be used

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'
 }
ELIP
  • 311
  • 6
  • 19
  • `However I expect to get directory only.`. Strange. I read: `FileChooser`. And where in the code do you get a path to a file? – blackapps Oct 07 '19 at 06:31
  • medthod _addFileChooserFragment();_ with FileChooser.Builder .... you can see.I want to get Directory . This program get particular path of file and display by Toast.makeText(MainActivity.this, path, Toast.LENGTH_SHORT).show(); – ELIP Oct 07 '19 at 06:38
  • I see no code where you get a path to a file and do something with it. Please Toast the path or log it. I would like to se that. – blackapps Oct 07 '19 at 06:39
  • Please read the documentation: `Android File Chooser is a simple and customizable file/directory chooser fragment which you can use in your apps to let your users select a file or directory based on your needs.`. Especially the words: `customizable file/directory chooser`. – blackapps Oct 07 '19 at 06:42
  • `you can see.I want to get Directory` ? Where can i see that? On the contrary i clearly see you want a file type with `FileChooser.ChooserType.FILE_CHOOSER`. – blackapps Oct 07 '19 at 06:47
  • Please change your android-studio tag to android. Your problem has nothing to do with Android Studio. – blackapps Oct 07 '19 at 06:51

0 Answers0