我一直有一个问题,该网站上的其他人已经。我已经尝试过这些解决方案,但他们不适合我。写入SD卡时Android csv文件不可见
我目前正在开发一个应用程序,它会根据用户输入创建一个csv文件。当设备通过USB连接到PC时,该文件需要由用户检索。保存代码功能正常工作,但是当设备连接时,文件在PC上不可见。
这是应用程序的完整保存的代码片段:
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());
}
该文件被写入到外部存储,虽然我可以看到更多的空间占用,文件本身仍然是不可见的。
任何帮助什么可能是问题将不胜感激。谢谢!
嗨,我试过你的解决方案,它似乎并没有正常工作。因为我添加的Toast从未显示过。 扫描需要多长时间,因为在检查之前我可能没有等待足够长的时间。 谢谢你的帮助! –
您是否能够在控制台上看到带有路径的扫描完整文本?这可能需要一段时间。 – MobileEvangelist
我还是没有办法,但我意识到我实际上是将它保存在内部的DCIM目录中,尽管没有你的帮助,它仍然是不可见的。感谢您的帮助! –