Gunaseelan Gunaseelan - 2 months ago 6
Android Question

Folders not found in file browing

I have tried the following code.

public class MainActivity extends GlobalActivity implements CompetitionListAdapter.OnOperationSelectedListener {

private String path = "";
private RecyclerView recyclerView;
private boolean isOnBackPressed = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initLogger("MainActivity");
recyclerView = (RecyclerView) findViewById(R.id.listRecyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
//path = "/storage/emulated/0/Download/pictures"; //When I pass this, then I can get the list of files in this folder.
loadFolders(path);
}

private void loadFolders(String path) {
if (this.path.isEmpty()) {
this.path = path;
} else if (!isOnBackPressed) {
this.path += "/" + path; //But when I make same the path dynamically from here file2 is always null.
}
writeLog(this.path);
File f = new File(path);
File file2[] = f.listFiles();
ArrayList<String> folders = new ArrayList<>();
if (file2 != null) {
for (File file1 : file2) {
folders.add(file1.getName());
}
} else {
writeLog("fList is empty");
}
recyclerView.setAdapter(new CompetitionListAdapter(folders, this));
}

@Override
public void onOperationSelected(CompetitionListAdapter.Operation operation, String name) {
isOnBackPressed = false;
loadFolders(name);
}

@Override
public void onBackPressed() {
String paths[] = this.path.split("/");
if (path.length() > 1) {
path = "";
for (int i = 1; i < paths.length - 1; i++) {
path += "/" + paths[i];
}
isOnBackPressed = true;
loadFolders(path);
return;
}
super.onBackPressed();
}
}


So the above code works at start the app... If I choose any folder from the list then I make the path at
loadFolders
dynamically, but this returns null always.

Any help will be highly appreciated.

Answer

you are pass folder name list in your adapter that's why you got folder name instance of folder_path in

   @Override
    public void onOperationSelected(CompetitionListAdapter.Operation operation, String name) {
        isOnBackPressed = false;
        loadFolders(name);
    }

replace

folders.add(file1.getName());

with folders.add(file1.getAbsolutePath());