android 获取assets目录下文件的路径

2,203 阅读1分钟
  1. 暴力直接获取
     String path = "file:///android_asset/icon_wm.png";
    
  2. 先把文件读成流,在转化地址
    String path = null;
    InputStream abpath = getClass().getResourceAsStream("/assets/icon_wm.png");
     try {
             path = new String(InputStreamToByte(abpath));
     }catch (Exception e){
            e.printStackTrace();
     }
     
    private byte[] InputStreamToByte(InputStream is) throws IOException {
            ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
            int ch;
            while ((ch = is.read()) != -1) {
                bytestream.write(ch);
            }
            byte imgdata[] = bytestream.toByteArray();
            bytestream.close();
            return imgdata;
        }