How to get reference to a file inside a package

2019-06-05 13:09发布

问题:

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...



I tried:

  1. /kanji.xls
  2. /Kanji database/kanji.xls
  3. //Kanji database//kanji.xls
  4. kanji.xls

回答1:

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");