1

I have an xls file in my assets folders which I would like to open using the jexcelapi library. I have set the path to the file as ("file:///android_asset/jxlrwtest.xls") and even tried creating an xls file with a different name in my root folder using New-> File and accessing it directly but still i get the error java.io.FileNotFoundException "(No such file or directory)" each time I run the emulator. I also made sure I refreshed the path each time I tried moving files. My entire class for reading the excel file is

package com.example.kirikedictionary;

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.content.Context;

import jxl.Cell;
import jxl.CellType;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class ReadExcel {

  private String inputFile;
  protected Context context;

  public ReadExcel(Context context){
      this.context = context.getApplicationContext();
  }


  public void setInputFile(String inputFile) {
    this.inputFile = inputFile;
  }

  public void read() throws IOException  {
    File inputWorkbook = new File(inputFile);
    Workbook w;
    try {
      w = Workbook.getWorkbook(inputWorkbook);
      // Get the first sheet
      Sheet sheet = w.getSheet(0);
      // Loop over first 10 column and lines

      for (int j = 0; j < sheet.getColumns(); j++) {
        for (int i = 0; i < sheet.getRows(); i++) {
          Cell cell = sheet.getCell(j, i);
          CellType type = cell.getType();
          if (type == CellType.LABEL) {
            System.out.println("I got a label "
                + cell.getContents());
          }

          if (type == CellType.NUMBER) {
            System.out.println("I got a number "
                + cell.getContents());
          }

        }
      }
    } catch (BiffException e) {
      e.printStackTrace();
    }
  }

  public void main(String[] args) throws IOException {
    ReadExcel test = new ReadExcel(context);
    test.setInputFile("file:///android_asset/jxlrwtest.xls");
    test.read();
  }

} 

and the error I keep getting is

12-30 01:36:59.386: W/System.err(1851): java.io.FileNotFoundException: file:/android_asset/jxlrwtest.xls: open failed: ENOENT (No such file or directory) 12-30 01:36:59.393: W/System.err(1851): at libcore.io.IoBridge.open(IoBridge.java:456) 12-30 01:36:59.393: W/System.err(1851): at java.io.FileInputStream.(FileInputStream.java:76) 12-30 01:36:59.394: W/System.err(1851): at jxl.Workbook.getWorkbook(Workbook.java:213) 12-30 01:36:59.394: W/System.err(1851): at jxl.Workbook.getWorkbook(Workbook.java:198) 12-30 01:36:59.394: W/System.err(1851): at com.example.kirikedictionary.ReadExcel.read(ReadExcel.java:33) 12-30 01:36:59.394: W/System.err(1851): at com.example.kirikedictionary.ReadExcel.main(ReadExcel.java:62) 12-30 01:36:59.394: W/System.err(1851): at com.example.kirikedictionary.MainActivity$3.onItemClick(MainActivity.java:148) 12-30 01:36:59.394: W/System.err(1851): at android.widget.AdapterView.performItemClick(AdapterView.java:300) 12-30 01:36:59.395: W/System.err(1851): at android.widget.AbsListView.performItemClick(AbsListView.java:1143) 12-30 01:36:59.416: W/System.err(1851): at android.widget.AbsListView$PerformClick.run(AbsListView.java:3044) 12-30 01:36:59.416: W/System.err(1851): at android.widget.AbsListView$3.run(AbsListView.java:3833) 12-30 01:36:59.416: W/System.err(1851): at android.os.Handler.handleCallback(Handler.java:739) 12-30 01:36:59.417: W/System.err(1851): at android.os.Handler.dispatchMessage(Handler.java:95) 12-30 01:36:59.432: W/System.err(1851): at android.os.Looper.loop(Looper.java:135) 12-30 01:36:59.432: W/System.err(1851): at android.app.ActivityThread.main(ActivityThread.java:5221) 12-30 01:36:59.432: W/System.err(1851): at java.lang.reflect.Method.invoke(Native Method) 12-30 01:36:59.432: W/System.err(1851): at java.lang.reflect.Method.invoke(Method.java:372) 12-30 01:36:59.433: W/System.err(1851): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 12-30 01:36:59.433: W/System.err(1851): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 12-30 01:36:59.433: W/System.err(1851): Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory) 12-30 01:36:59.434: W/System.err(1851): at libcore.io.Posix.open(Native Method) 12-30 01:36:59.435: W/System.err(1851): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186) 12-30 01:36:59.451: W/System.err(1851): at libcore.io.IoBridge.open(IoBridge.java:442) 12-30 01:36:59.452: W/System.err(1851): ... 18 more

Please help as I do not want to have to use external sd card files and I've been stuck here for a while.

Kosy Onyenso
  • 109
  • 2
  • 14
  • Please, import your project to Android Studio. Eclipse is deprecated and it would generate more like this problems – piotrek1543 Dec 30 '15 at 00:44

1 Answers1

5

You can use file:///android_asset/someName only as an URL for WebView. It is not a file name recognized by the file system.

Use Context.getAssets to get an AssetManger instance, and use AssetManager.open to get an InputStream for an assets file.

If you need to provide a File instance, copy the file from assets into the app's private directory.

Rediska
  • 1,392
  • 10
  • 14