我有一个代码:
File myFile = new File("/sdcard/Pictures/MyCameraApp/1.jpg");
FileInputStream fin = null;
// create FileInputStream object
try {
fin = new FileInputStream(myFile);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte fileContent[] = new byte[(int)myFile.length()];
// Reads up to certain bytes of data from this input stream into an array of bytes.
try {
fin.read(fileContent);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//create string from byte array
String s = new String(fileContent);
return new NanoHTTPD.Response(HTTP_OK, "image/jpeg", s);
但是当我使用浏览器时,我看到一个损坏的 JPEG 文件。我究竟做错了什么?我希望用户在输入某个地址时查看 1.jpg 文件
编辑:将代码更改为:
File myFile = new File("/sdcard/Pictures/MyCameraApp/1.jpg");
FileInputStream fin = null;
// create FileInputStream object
try {
fin = new FileInputStream(myFile);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int bytesRead;
try {
while ((bytesRead = fin.read(b)) != -1) {
bos.write(b, 0, bytesRead);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] fileContent = bos.toByteArray();
//create string from byte array
String s = new String(fileContent);
return new NanoHTTPD.Response(HTTP_OK, "image/jpg", s);
仍然不起作用.....当我进入页面时,我得到损坏的 jpg 文件,(如果我写 image/jpeg 而不是 image/jpg 或 text/html,则将文本另存为 jpg 文件也不起作用。非常适合文本:\