简体   繁体   中英

Issue in path to a file in assets folder of android

i know it is basic but stuck. i gave a file path like this

String path = "file:///android_asset/xls/x.xlsx";
File f = new File(path);

I am getting Filenotfound exception and path it is showing file:/android_asset/xls/x.xlsx which is wrong because i want "file:///android_asset/xls/x.xlsx" . Any help ? thanks

you can try something like that

InputStream is = getResources().getAssets().open("x.xlsx");

if your file have inner folder in assets folder then try this way

AssetManager am = getAssets();
InputStream is = am.open(file:///android_asset/xls/x.xlsx);

or try like this

InputStream is = getResources().getAssets().open("xls/x.xlsx");
File file = createFileFromInputStream(is);

if you want the file object then use this

final File createFileFromInputStream(InputStream is);

try{
   File f=new File(my_file_name);
   OutputStream outputStream=new FileOutputStream(f);
   byte buffer[]=new byte[1024];
   int length=0;

   while((length=inputStream.read(buffer))>0) {
     outputStream.write(buffer,0,length);
   }
   outputStream.close();
   inputStream.close();

}catch (IOException e){
      //Logging exception
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM