-2

I am developing an android app and I want to read an xsl file. I have following code

    File rootPath=Environment.getExternalStorageDirectory();
   try {
    File randomContactsFile = new File(rootPath.getPath()+"/assets/LoginTest.xlsx");
    System.out.println(randomContactsFile.exists());
    }catch(Exception e){
    }

randomContactsFile.exists() returns false and I'm getting file not found exception.

Please help.

Thanks

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
itin
  • 430
  • 3
  • 9
  • 20

2 Answers2

3

May This Help you:

You will not be able to access assets/ at runtime using File.
You access assets/ at runtime using AssetManager, which you can get via getResources().getAssets().

Like:

AssetManager assetManager = getResources().getAssets();

And use assetManager.open(your file name);

Edit:

InputStream is = //Open file, and get inputstream
Workbook workBook = WorkbookFactory.create(is);
int totalSheets = workBook.getNumberOfSheets();
for (int i = 0; i <= totalSheets - 1; i++) {
  Sheet sheet = workBook.getSheetAt(i);
  // Do something with the sheet
}
Bhavin Nattar
  • 3,189
  • 2
  • 22
  • 30
  • Thanks, but my eclipse not recognizing getAssets() method. do we need to use context? Actually i want to open a xls file with FileInputStream – itin Aug 01 '13 at 11:01
  • @itin: yes buddy it uses context.. if your class extends Activity then it will not require otherwise you will need it... – Bhavin Nattar Aug 01 '13 at 11:03
  • but actually i can not extends the Activity class as i have already extends the ActivityInstrumentationTestCase2. I want to read xls file using poi which need FileInputStream. – itin Aug 01 '13 at 15:28
  • my code is fis = new FileInputStream(path); workbook = new XSSFWorkbook(fis); so context.getResources().getAssets().open not work i think... please advice... – itin Aug 01 '13 at 15:32
  • buddy see the edit and refer this link it may help you: http://stackoverflow.com/questions/3387478/how-to-read-excel-file-using-jxl-2-6-12-jar – Bhavin Nattar Aug 02 '13 at 05:08
0

Solved:

if you get this exception even when you are sure that you have included all the jars then create a libs folder in the project root directory and copy all jars in libs folder. the add these jars in java build path (select all jars and right click add jars.)

itin
  • 430
  • 3
  • 9
  • 20