Gunaseelan
Gunaseelan

Reputation: 15535

Folders not found in file browsing

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.

Upvotes: 1

Views: 57

Answers (3)

Kuldeep Kulkarni
Kuldeep Kulkarni

Reputation: 796

Make sure you have to use below permission:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

If you used already, make sure if path is correct or not.

Upvotes: 0

prakash ubhadiya
prakash ubhadiya

Reputation: 1271

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

Upvotes: 1

GoneUp
GoneUp

Reputation: 385

Don't create a new CompetitionListAdapter every time you need to update the data inside. I don't know how your Adapter is working, but you should make a method to update the list inside/add items and then you you call notifyDataSetChanged to update the view.

Upvotes: 0

Related Questions