【发布时间】:2016-07-04 06:20:06
【问题描述】:
我正在尝试使用 Jexcel API 创建一个 excel 文件并使用我手机上的应用程序写入它。当我运行该应用程序时,它会抛出一个FileNotFoundException。我什至尝试根据另一个此类问题的答案创建一个文本文件,但它会引发相同的错误。我已经在清单中给予了适当的权限,但我似乎仍然无法查明问题所在。
请帮忙。
这是我的代码
public WritableWorkbook createWorkbook(String fileName){
//Saving file in external storage
File sdCard = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File directory = new File(sdCard.getAbsolutePath() + "/bills");
//create directory if not exist
if(!directory.isDirectory()){
directory.mkdirs();
}
//file path
file= new File(directory, fileName);
if (file.exists())
Log.e(taf,"file created");
else
Log.e(taf,"file not created");
WorkbookSettings wbSettings = new WorkbookSettings();
wbSettings.setLocale(new Locale("en", "EN"));
wbSettings.setUseTemporaryFileDuringWrite(true);
WritableWorkbook workbook;
workbook=null;
try {
workbook = Workbook.createWorkbook(file, wbSettings);
Log.i(taf,"workbook created");
//Excel sheet name. 0 represents first sheet
WritableSheet sheet = workbook.createSheet("MyShoppingList", 0);
try {
sheet.addCell(new Label(0, 0, "Subject")); // column and row
sheet.addCell(new Label(1, 0, "Description"));
String title = "blaj";
String desc = "nxjdncj";
int i = 1;
sheet.addCell(new Label(0, i, title));
sheet.addCell(new Label(1, i, desc));
} catch (WriteException e) {
e.printStackTrace();
}
workbook.write();
try {
workbook.close();
} catch (WriteException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
return workbook;
}
执行时,会打印日志消息“文件未创建”。我是 android 新手,所以请指出最基本的问题。
谢谢。
【问题讨论】:
-
你的安卓版本是6.0?
-
是的。它适用于预棉花糖设备。我已经读过,对于棉花糖设备,您必须添加运行时权限,但我不知道如何添加这些权限。
标签: android file-io filenotfoundexception