浏览器请求android HttpService显示图像
我已经在android上创建了http服务。现在我正试图让HttpService在浏览器中显示一些图像。我在浏览器中写一个url(例如(我玩emulator))。http服务向我发送html,如下所示:浏览器请求android HttpService显示图像,android,image,browser,webserver,httpservice,Android,Image,Browser,Webserver,Httpservice,我已经在android上创建了http服务。现在我正试图让HttpService在浏览器中显示一些图像。我在浏览器中写一个url(例如(我玩emulator))。http服务向我发送html,如下所示: <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> <title>File Upload</title>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>File Upload</title>
</head>
<body>
<td><img src='127.0.0.1:6789/1.png'/></td><br>
<td><img src='127.0.0.1:6789/2.png'/></td><br>
</body>
<img src='http://127.0.0.1:6789/2.png'/>
ImageCommandHandler:
@Override
public void handle(HttpRequest request, HttpResponse response,
HttpContext httpContext) throws HttpException, IOException {
final InputStream is = GetInpuStreamFromResource(getContext(),
R.drawable.back);
HttpEntity entity = new EntityTemplate(new ContentProducer() {
public void writeTo(final OutputStream outstream) throws IOException {
int bufSize = 0;
byte[] buf = new byte[32768];
while(-1!=(bufSize=is.read(buf))){
outstream.write(buf,0,bufSize);
}
outstream.flush();
outstream.close();
is.close();
}
});
response.setHeader("Content-Type", "image/*");
response.setEntity(entity);
或者以另一种方式
@Override
public void handle(HttpRequest request, HttpResponse response,
HttpContext httpContext) throws HttpException, IOException {
final File f= new File("/sdcard/Tulips.jpg");
String contentType = URLConnection.guessContentTypeFromName(f.getAbsolutePath());
FileEntity entity = new FileEntity(f, contentType);
response.setHeader("Content-Type", contentType);
response.setEntity(entity);
}
我两种方法都试过了,但还是没有任何图像!怎么了?如何使用例如JavaScript的文件?
谢谢。它工作得很好!我的错误是我写了:
<img src='127.0.0.1:6789/2.png'/>
但它返回空白页,因为我错过了http://
我应该这样写:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>File Upload</title>
</head>
<body>
<td><img src='127.0.0.1:6789/1.png'/></td><br>
<td><img src='127.0.0.1:6789/2.png'/></td><br>
</body>
<img src='http://127.0.0.1:6789/2.png'/>
谢谢 看一看