第一种方法:
String path = "file:///android_asset/文件名";
第二种方法:
InputStream abpath = getClass().getResourceAsStream("/assets/文件名");
若要想要转换成String类型
String path = new String(InputStreamToByte(abpath ));
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;
String path = "file:///android_asset/文件名";
第二种方法:
InputStream abpath = getClass().getResourceAsStream("/assets/文件名");
若要想要转换成String类型
String path = new String(InputStreamToByte(abpath ));
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资产文件读取与转换
本文介绍两种在Android中加载资产文件的方法,并演示如何将输入流转换为字符串。第一种方法使用绝对路径加载文件,第二种方法通过getClass().getResourceAsStream()获取资源。此外,还提供了一个实用函数用于从输入流中读取数据并将其转换为字节数组。
3805

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



