小编kes*_*har的帖子

尝试在Android中创建文件时出现FileNotFoundException

我正在尝试使用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 …
Run Code Online (Sandbox Code Playgroud)

file-io android filenotfoundexception

2
推荐指数
1
解决办法
1875
查看次数

标签 统计

android ×1

file-io ×1

filenotfoundexception ×1