String path ="";
InputStream abpath = getClass().getResourceAsStream("/assets/ic_launcher.png");
try {
path = new String(InputStreamToByte(abpath ));
} catch (IOException e) {
e.printStackTrace();
}
InputStream abpath = getClass().getResourceAsStream("/assets/ic_launcher.png");
try {
path = new String(InputStreamToByte(abpath ));
} catch (IOException e) {
e.printStackTrace();
}
//文件流转Byte
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;
}
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;
}
本文介绍如何使用Java从类路径中获取资源并将其转换为字节流,详细步骤包括创建输入流、读取资源内容、将字节流转换为字符串等关键操作。
1146

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



