我有一个方法可以在我的设备上创建目录和文件列表。我注意到它的工作原理很奇怪,我不明白为什么。这里的代码:
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);
}
}
});
以及如何创建我的文件夹列表:
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);
}
在我的模拟器(GenyMotion)上运行良好,在我的手机Huawei上,我在第二台设备上只能看到一个空对话框,没有任何文件夹和文件,小米-我可以看到与第一台设备类似的情况。为什么它与众不同,我该如何解决?