0

I have a method which creates a list of directories and files on my device. I noticed that it works strange and I can't understand why. Here the code:

root = new File(Environment.getExternalStorageDirectory().getPath());
curFolder = root;

dialog = new Dialog(WriteResponseMess.this);
                dialog.setContentView(R.layout.dialog_layout);
                dialog.setCanceledOnTouchOutside(true);

                textFolder = dialog.findViewById(R.id.folder);
                buttonUp = dialog.findViewById(R.id.up);
                buttonUp.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        ListDir(curFolder.getParentFile());
                    }
                });


                dialog_ListView = dialog.findViewById(R.id.dialoglist);

                dialog_ListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @RequiresApi(api = Build.VERSION_CODES.O)
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                        File selected = new File(curFolder, fileList.get(position));
                        if (selected.isDirectory()) {
                            ListDir(selected);
                        } else {
                            if (array.size() == 0) {
                                array = new JsonArray();
                                array = uploadFiles(array, selected.getName(), convertFileToString(selected.getPath()));
                            } else {
                                if (array.toString().contains(selected.getName())) {
                                    Log.w("MY_TAG", "CONTAINS");
                                    Toast.makeText(WriteResponseMess.this, R.string.attaching_message, Toast.LENGTH_SHORT).show();
                                } else {
                                    array = uploadFiles(array, selected.getName(), convertFileToString(selected.getPath()));
                                }

                            }
                            //Log.w("MY_TAG", String.valueOf(array.getAsJsonArray()));
                            ms.setArray(array);
                        }
                    }
                });

and here how I create a list of my folders:

void ListDir(File f) {
        if (f.equals(root)) {
            buttonUp.setEnabled(false);
        } else {
            buttonUp.setEnabled(true);
        }

        curFolder = f;
        textFolder.setText(f.getPath());
        String[] files = f.list();
        fileList.clear();
        if (files != null) {
            fileList.addAll(Arrays.asList(files));
        }
        ArrayAdapter<String> directoryList = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, fileList);
        dialog_ListView.setAdapter(directoryList);
    }

on my emulator (GenyMotion) it works pretty good, on my mobile phone Huawei I see only empty dialog without any folders and files, on my second device - xiaomi I can see similar situation to my first device. Why it works different and how I can fix it?

Andrew
  • 1,947
  • 2
  • 23
  • 61
  • Are you sure you have permission to read external storage on those devices in which it's failing? Not just in the manifest, but also runtime permissions requests, if necessary? – Mike M. Nov 27 '18 at 23:29
  • yes, of course, sometimes my files and folders are show :) – Andrew Nov 28 '18 at 04:12
  • What exactly do you mean by "sometimes"? Are you certain you're getting the permissions _before_ trying to read from storage? – Mike M. Nov 28 '18 at 04:17
  • sometimes means that on my emulator I can see my folders and on my real devices I can't – Andrew Nov 28 '18 at 04:26
  • 1
    That's why I was asking specifically about those devices. Are you certain you have permission, and that you're obtaining that permission before trying to list files? How exactly are you determining that you have the required permission? – Mike M. Nov 28 '18 at 04:28
  • 1
    thank you @MikeM., I gave that permission and I saw my folders, that's why now I have to create special request for getting that permission :) – Andrew Nov 28 '18 at 05:55

0 Answers0