- 暴力直接获取
String path = "file:///android_asset/icon_wm.png"; - 先把文件读成流,在转化地址
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; }
android 获取assets目录下文件的路径
最新推荐文章于 2024-07-10 03:09:55 发布
本文介绍了一种在Android环境中从assets目录读取文件并将其转换为字符串的方法。通过使用InputStream和ByteArrayOutputStream,可以将assets文件转化为内存中的字节数组,进而转换为字符串路径。此过程涉及到异常处理,确保了文件读取的安全性和稳定性。
3814

被折叠的 条评论
为什么被折叠?



