The Dark Knight
The Dark Knight

Reputation: 5585

Issue while reading excel workbook from assets folder

Normally i like to keep my files for my app in assets and then i read my files from that folder. So, when i tried to read my excel file(which is a 97-2003 work book) from assets folder, it failed(Process - 1). However, when i push the file to /mnt/sdcard/ using adb.exe and then try to read from there, it works(Process - 2).

Code for Process - 1 :

File inputWorkbook = new File((assetManager.open("myFile.xls")).toString());

Exception Shown :

java.io.FileNotFoundException: /android.content.res.AssetManager$AssetInputStream@40cd4ce0: open failed: ENOENT (No such file or directory)

Code for Process - 2 :

File inputWorkbook = new File(Environment.getExternalStorageDirectory()+"/myFile.xls");

1> Can any one tell me, what i am doing wrong ?

2> Also, is there any limitation to file size for the files i keep in assets ? If so, then can i keep it some where else ?

Thanks .

EDIT : I have got this one to work as :

InputStream dbInputStream = context.getAssets().open("myFile.xls");

Still, i will like to know any alternative solutions, if some one has such solutions as well as the answer to my 2nd question .

Upvotes: 1

Views: 1459

Answers (1)

itin
itin

Reputation: 450

One way (Read from assets folder):

AssetManager assetManager =   m.getInstrumentation().getContext().getResources().getAssets();
InputStream in = assetManager.open(filename);

Another way (Read from /mnt/sdcard/Android/data/file)

File file = new File(m.getInstrumentation().getContext().getExternalFilesDir(null), filename);
fis = new FileInputStream(file);

Upvotes: 1

Related Questions