我想将downloads文件夹添加到我的代码中, 这是我的助手类(文件路径):
//"storage/emulated/0" = Environment.getExternalStorageDirectory().getPath().
public String ROOT_DIR = Environment.getExternalStorageDirectory().getPath();
public String PICTURES = ROOT_DIR + "/Pictures";
public String CAMERA = ROOT_DIR + "/DCIM/camera";
public String DOWNLOADS = ROOT_DIR + "/Download";
这是我使用此路径的班级
private void init(){
FilePaths filePaths = new FilePaths();
//check for other folders indide "/storage/emulated/0/pictures"
if(FileSearch.getDirectoryPaths(filePaths.PICTURES) != null){
directories = FileSearch.getDirectoryPaths(filePaths.PICTURES);
}
ArrayList<String> directoryNames = new ArrayList<>();
for(int i = 0; i < directories.size(); i++){
int index = directories.get(i).lastIndexOf("/");
String string = directories.get(i).substring(index);
directoryNames.add(string);
}
directories.add(filePaths.CAMERA);
directories.add(filePaths.DOWNLOADS);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_spinner_item, directories); // WARUM
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
directorySpinner.setAdapter(adapter);
directorySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.d(TAG, "onItemClick: selected: " + directories.get(position));
//setup image grid for the directory chosen
setupGridView(directories.get(position));
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
如果我将下载部分切掉,我会得到其他路径的所有文件,但是如果下载部分在我的代码中,则会收到异常,因为稍后下载的文件没有添加到arrayList中。
我需要正确的下载路径。 我发现了:
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
但这会返回一个文件,并且我的帮助器类中需要一个字符串。
编辑:
对不起,这是我的错。我得到了例外,因为我的其他目录中没有任何图片。在插入一个文件夹后,将显示我的下载文件夹。我立即尝试并解决了此错误。
答案 0 :(得分:0)
您可以像这样从文件获取字符串路径
money_thread = threading.Thread(target=money_loop, args=(), daemon=True)