【发布时间】:2017-02-17 05:57:06
【问题描述】:
我遇到了网站上其他人已经遇到的问题。我已经尝试了这些解决方案,但它们对我不起作用。
我目前正在开发一个应用程序,该应用程序将根据用户输入创建一个 csv 文件。当设备通过 USB 连接到 PC 时,用户需要可以检索该文件。保存代码功能正常,但连接设备后,文件在 PC 上不可见。
这是应用的完整保存代码sn-p:
FileWriter fileWriter = null;
File sdCard = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
File dir = new File(sdCard.getAbsolutePath() + "/appFolder/");
File dir2 = new File(sdCard.getAbsolutePath() + "/appFolder/appSubFolder/");
dir.mkdirs();
dir2.mkdirs();
fileName = clientName + " " + agentName + ".csv";
path = sdCard.getAbsolutePath() + "/appFolder/appSubFolder/";
file = new File(dir2, fileName);
dir.setReadable(true);
dir.setWritable(true);
dir.setExecutable(true);
dir2.setReadable(true);
dir2.setWritable(true);
dir2.setExecutable(true);
file.setReadable(true);
file.setWritable(true);
file.setExecutable(true);
this.clientName = clientName;
this.agentName = agentName;
BufferedWriter bufferedWriter = null;
try {
if(!dir.exists()){
dir.createNewFile();
Toast.makeText(context,"dir created", Toast.LENGTH_SHORT).show();
}
if(!dir2.exists()){
dir2.createNewFile();
Toast.makeText(context,"dir2 created", Toast.LENGTH_SHORT).show();
}
if (!file.exists() )
{
file.createNewFile();
Toast.makeText(context,"file created", Toast.LENGTH_SHORT).show();
}
/*
fileWriter = new FileWriter(file, true);
bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(fullOrder);
fileWriter.flush();
bufferedWriter.flush();
bufferedWriter.close();
fileWriter.close();
*/
FileOutputStream fileOutputStream = new FileOutputStream(file);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);
outputStreamWriter.append(fullOrder);
outputStreamWriter.close();
fileOutputStream.close();
MediaScannerConnection.scanFile(context, new String[]{file.toString()}, null, null);
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(dir)));
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(dir2)));
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
} catch (IOException ioEx) {
Toast.makeText(context, ioEx.toString(), Toast.LENGTH_SHORT).show();
Log.d("mTag", "msg");
Log.d("Exception ioEx 1", ioEx.toString());
} catch (Exception ex) {
Toast.makeText(context, ex.toString(), Toast.LENGTH_SHORT).show();
Log.d("mTag", "msg");
Log.d("Genereic ex saveToFile", ex.toString());
}
文件正在写入外部存储,虽然我可以看到占用了更多空间,但文件本身仍然不可见。
任何可能出现问题的帮助将不胜感激。谢谢!
【问题讨论】:
标签: java android csv android-external-storage file-writing