How to get the android Path string to a file on Assets folder?

前端 未结 4 716
野的像风
野的像风 2020-11-27 03:29

I need to know the string path to a file on assets folder, because I\'m using a map API that needs to receive a string path, and my maps must be stored on a

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-27 03:54

    AFAIK the files in the assets directory don't get unpacked. Instead, they are read directly from the APK (ZIP) file.

    So, you really can't make stuff that expects a file accept an asset 'file'.

    Instead, you'll have to extract the asset and write it to a seperate file, like Dumitru suggests:

      File f = new File(getCacheDir()+"/m1.map");
      if (!f.exists()) try {
    
        InputStream is = getAssets().open("m1.map");
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
    
    
        FileOutputStream fos = new FileOutputStream(f);
        fos.write(buffer);
        fos.close();
      } catch (Exception e) { throw new RuntimeException(e); }
    
      mapView.setMapFile(f.getPath());
    

提交回复
热议问题