ELIP
ELIP

Reputation: 311

How to get directory

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'
 }

Upvotes: 0

Views: 128

Answers (0)

Related Questions