我的android java应用程序在外部存储中看不到任何.xml文件



我正在制作一个应用程序来测试Opencv haarcascades,但我的应用程序似乎看不到.xml文件。我的权限包括:


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

我收集文件名和路径的代码是:


public ArrayList<String []> getLongAndShortFileHandles(String root_dir) {
ArrayList<String[]> ret = new ArrayList<>();
String path = Environment.getExternalStorageDirectory().toString()+"/"+root_dir;
File directory = new File(path);
File[] files = directory.listFiles();
Log.e(TAG, "getLongAndShortFileHandles: Has dir "+path+", with "+((files==null) ? "null" : files.length) + " number of files");
for (int i = 0; i < Objects.requireNonNull(files).length; i++)
{
Log.e(TAG, "getLongAndShortFileHandles: HAS FILE "+files[i].getName());
String [] temp = new String [] {files[i].getAbsolutePath(),files[i].getName()};
ret.add(temp);
}
return ret;
}

调试显示,当我调用这个函数来填充我的RecycleView子类时,没有文件存在,如下所示:


this.cascade_selector = (RecyclerView) findViewById(R.id.cascade_view);
this.cascade_selector.setAdapter(new FileAdaptah(getLongAndShortFileHandles("cascade_app/cascades"),this, CASCADE));

this.xml_ano_selector = (RecyclerView) findViewById(R.id.annotation_view);
this.xml_ano_selector.setAdapter(new FileAdaptah(getLongAndShortFileHandles("cascade_app/annotations"),this, ANNOTATION_XML));

应用程序中的结果确实显示了第二次调用的输出,但没有显示第一次调用的结果。如果第一个文件夹被任何其他文件夹(如DCIM或Music(替换,则会显示文件。

但是,如果我将.xml文件添加到任何目录中,它都不会显示。

编辑:

我在玩不同的目录和文件,并确定问题进一步,然后只有.xml,它似乎是我能想到的所有ascii编码的文本文件格式。所有二进制文件,如.jpg、.wav、.mp4、.bin等,放在同一目录中时都可以在中找到。

EDIT(2(:所以我已经确定,如果我将任何文本文件扩展名重命名为任何二进制扩展名(例如50.xml -> 50.jpg(,脚本就会检测到它们。

这促使我尝试不同的文件名过滤器,但没有取得任何成功。

我也尝试过使用目录流,但没有用:


@RequiresApi(api = Build.VERSION_CODES.O)
public ArrayList<String[]> getFiles(String suffix, String root_dir){
ArrayList<String[]> ret = new ArrayList<String[]>();
try {
root_dir = Environment.getExternalStorageDirectory().toString() + "/" + root_dir;
Path dirName = Paths.get(root_dir);
DirectoryStream<Path> paths = Files.newDirectoryStream(dirName, "*.pdf");
paths.forEach(path -> {
Log.e(TAG, "getFiles: HAS FILE : "+path.toString());
if (path.endsWith(suffix)) {
String sPath = path.toString();
String[] splitted = sPath.split(File.separator);
ret.add(new String[]{sPath, splitted[splitted.length - 1]});
}
});
} catch (IOException exception) {
exception.printStackTrace();
}
return ret;
}

我开始怀疑这可能是一个错误,所以我提供了我的环境信息:我的android工作室是4.1.2版本,我的目标SDK是30,而我接受的最低版本是19。我的测试设备是一台带有android 11的Galaxy A40。

EDIT(3(:所以我注意到getFiles的第二个片段在复制原始片段时有剩余的glob表达式。出于兴趣,我修改并删除了它,但在任何情况下都没有改变。

EDIT(4(:所以我尝试使用以下代码直接访问文件:


this.test = (Button) findViewById(R.id.select_to_test);
this.test.setOnClickListener(v -> {
String path = Environment.getExternalStorageDirectory().toString()+"/cascade_app/cascades/50.xml";
Intent intent = new Intent(v.getContext(),CheckCascade.class);
intent.putExtra("fh",path);
File debugFile = new File(path);
Toast.makeText(this, "File "+path+" does "+((debugFile.exists())? "exist":"not exist"), Toast.LENGTH_SHORT).show();
startActivity(intent);
});
}

而且它似乎检测到它没有问题。

所以过了一段时间,一位好心人建议我检查一下我的应用程序是否需要此权限。这解决了问题!我希望这能帮助到有同样问题的人。

最新更新