gpt4 book ai didi

java - 在具有正确文件路径和权限的 Android 中找不到文件异常

转载 作者:行者123 更新时间:2023-12-02 09:58:27 24 4
gpt4 key购买 nike

我正在尝试读取已创建的 CSV 文件。路径变量显示 Android 应用程序文件夹中的正确路径。另外,我已经添加了读写外部存储权限,但我仍然收到文件未找到异常,如 Volley Error toast 消息中所示。

读取 CSV 代码:-

    String path = String.valueOf(this.getExternalFilesDir(null));
Log.d("Path",path+"/Final.csv");
File csvFILE = new File(path+"/","Final.csv");
try {
CSVReader csvREAD = new CSVReader(new
FileReader(path));
String[] csvLINE;
int skip = 0;
try {
Log.d("I was here","kk");
while((csvLINE = csvREAD.readNext())!=null)
{
if(skip > 0)//becasue first line is column headers
{
String PARAM_DISTRICT = csvLINE[0];
String PARAM_TALUKA = csvLINE[1];
String PARAM_VILLAGE = csvLINE[2];
String PARAM_FARMER_NAME = csvLINE[3];
String PARAM_CONTACT_NUMBER = csvLINE[4];
String PARAM_GAT_NUMBER = csvLINE[5];
String PARAM_CROP_NAME = csvLINE[6];
String PARAM_LANDUSE = csvLINE[7];
String PARAM_SOIL_TYPE = csvLINE[8];
int PARAM_SOIL_DEPTH = Integer.parseInt(csvLINE[9]);
double PARAM_LATITUDE = Double.parseDouble(csvLINE[10]);
double PARAM_LONGITUDE = Double.parseDouble(csvLINE[11]);
String PARAM_TIMESTAMP = csvLINE[12];

Data STUD_OBJECT = new Data(PARAM_DISTRICT,PARAM_TALUKA,PARAM_VILLAGE,
PARAM_FARMER_NAME,PARAM_CONTACT_NUMBER,PARAM_GAT_NUMBER,
PARAM_CROP_NAME,PARAM_LANDUSE,PARAM_SOIL_TYPE,PARAM_SOIL_DEPTH,
PARAM_LATITUDE,PARAM_LONGITUDE,PARAM_TIMESTAMP
);
dataArray.add(STUD_OBJECT);
}
else
{
skip ++;
}
}
} catch (IOException e) {
e.printStackTrace();
}

} catch (FileNotFoundException e) {
Toast.makeText(this, "Volley Error", Toast.LENGTH_SHORT).show();
}

CSV 文件是使用以下代码创建的:

    String Testfile_name="Final";
Testfile_name=Testfile_name+".csv";
File logFile = new File(this.getExternalFilesDir(null), Testfile_name);
if (!logFile.exists())
{
try
{
logFile.createNewFile();
String header= "District,Taluka,Village,Farmer_name,Contact_Number,Gat_Number,Crop_name,Landuse,Soil_Type,Soil_Depth,Latitude,Longitude,Timestamp";
appendLog(header);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}

最佳答案

这个:

File logFile = new File(this.getExternalFilesDir(null), Testfile_name);

不指向同一个地方:

String path="storage/emulated/0";
Log.d("Path",path+"/Final.csv");
File csvFILE = new File(path+"/","Final.csv");

使它们相同,最好使用代码的第一位和getExternalFilesDir()

关于java - 在具有正确文件路径和权限的 Android 中找不到文件异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55810297/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com