1

I have file kanji.xls. I would like to get that file through new File(); What should I write inside that constructor? I tried many options but didn't find correct answer...

enter image description here

I tried:

  1. /kanji.xls
  2. /Kanji database/kanji.xls
  3. //Kanji database//kanji.xls
  4. kanji.xls
Marek
  • 3,935
  • 10
  • 46
  • 70
  • 1
    can you post your code? – sanbhat Jun 03 '13 at 06:13
  • sanbhat whole code is workBook = Workbook.getWorkbook(new File("/kanji.xls")); I tried to put many things inside the File constructor, and I always get FileNotFoundException – Marek Jun 03 '13 at 06:14

1 Answers1

2

Use assets directory in your project to store this file and read file from there. Android – Read file from Assets will guide you.

if you don't wish to use assets directory,

Create directory as Kanji_Database instead of Kanji database. Then use below code.

File f= new File(Environment.getExternalStorageDirectory()
            + "/Android/data/" + getApplicationContext().getPackageName()
            + "/Kanji_Database/", "kanji.xls");
Bishan
  • 15,211
  • 52
  • 164
  • 258
  • your answer apears to be the best, but through assets I can only get a list of files or open a Stream. How can I use assets to get filepath that I can later use in new File()? – Marek Jun 03 '13 at 06:44
  • The files in the `assets` directory doesn't get unpacked. They are read directly from the `APK` ZIP file. So you have to extract to a byte array, write to a temp file and load that file.you can use `file:///android_asset/kanji.xls` for a url . – Bishan Jun 03 '13 at 07:21
  • I managed to do it with assets like this: workBook = Workbook.getWorkbook(assetManager.open("kanji_short.xls")); Thank you for your help :) Just to be sure, all folders that I put into my package will exist in getApplicationContext().getPackageName()? – Marek Jun 03 '13 at 07:55
  • Could you also tell me physical location of Assets folder? – Marek Jun 06 '13 at 00:21
  • It is in inside of APK file. so there is no physical path of the assets folder in your app.you can use `file:///android_asset/kanji.xls` to refer your file in assets using `WebView`. – Bishan Jun 06 '13 at 03:50
  • So I can't just locate it in my phone using Total Commander or similar application? Interesting. In that case resources folder could be better. – Marek Jun 06 '13 at 04:05
  • AFAIK you can't access `assets` folder via any file manager. if you want to get physical path to any file in your app, use resources folder. – Bishan Jun 06 '13 at 05:32