android怎么在服务器和客户端之间传输图片? 15
这是我的服务端的代码:StringimagePath="/home/shenlei/tupian/jiaofu.jpg";Filefile=newFile(imagePa...
这是我的服务端的代码:
String imagePath="/home/shenlei/tupian/jiaofu.jpg";
File file =new File(imagePath);
FileInputStream inputstream = new FileInputStream(file);
OutputStream out = resp.getOutputStream();
//拷贝输出
byte[] buf = new byte[1024];
int count = 0;
while ((count =inputstream.read(buf)) >= 0) {
out.write(buf, 0, count);
}
inputstream.close();
out.close();
}
这是客户端的接受的代码:
// 取得图片字节流
HttpEntity httpEntity = response.getEntity();
InputStream is = httpEntity.getContent();
bitmap = BitmapFactory.decodeStream(is);
is.close();
但是获取的图像没有现实出来:
ImageView image = (ImageView) findViewById(R.id.detail_img);
image.setImageBitmap(HttpUtil.bitmap);
这是log
07-28 12:52:13.861: D/libc(25884): [NET] getaddrinfo+,hn 13(0x3139322e313638),sn(),family 0,flags 4
07-28 12:52:13.861: D/libc(25884): [NET] getaddrinfo-, SUCCESS
07-28 12:52:13.861: D/libc(25884): [NET] getaddrinfo+,hn 13(0x3139322e313638),sn(),family 0,flags 4
07-28 12:52:13.861: D/libc(25884): [NET] getaddrinfo-, SUCCESS
07-28 12:52:13.861: D/libc(25884): [NET] getaddrinfo+,hn 13(0x3139322e313638),sn(),family 0,flags 4
07-28 12:52:13.861: D/libc(25884): [NET] getaddrinfo-, SUCCESS
07-28 12:52:13.871: I/global(25884): call createSocket() return a new socket.
07-28 12:52:13.871: D/libc(25884): [NET] getaddrinfo+,hn 13(0x3139322e313638),sn(),family 0,flags 4
07-28 12:52:13.871: D/libc(25884): [NET] getaddrinfo-, SUCCESS 展开
String imagePath="/home/shenlei/tupian/jiaofu.jpg";
File file =new File(imagePath);
FileInputStream inputstream = new FileInputStream(file);
OutputStream out = resp.getOutputStream();
//拷贝输出
byte[] buf = new byte[1024];
int count = 0;
while ((count =inputstream.read(buf)) >= 0) {
out.write(buf, 0, count);
}
inputstream.close();
out.close();
}
这是客户端的接受的代码:
// 取得图片字节流
HttpEntity httpEntity = response.getEntity();
InputStream is = httpEntity.getContent();
bitmap = BitmapFactory.decodeStream(is);
is.close();
但是获取的图像没有现实出来:
ImageView image = (ImageView) findViewById(R.id.detail_img);
image.setImageBitmap(HttpUtil.bitmap);
这是log
07-28 12:52:13.861: D/libc(25884): [NET] getaddrinfo+,hn 13(0x3139322e313638),sn(),family 0,flags 4
07-28 12:52:13.861: D/libc(25884): [NET] getaddrinfo-, SUCCESS
07-28 12:52:13.861: D/libc(25884): [NET] getaddrinfo+,hn 13(0x3139322e313638),sn(),family 0,flags 4
07-28 12:52:13.861: D/libc(25884): [NET] getaddrinfo-, SUCCESS
07-28 12:52:13.861: D/libc(25884): [NET] getaddrinfo+,hn 13(0x3139322e313638),sn(),family 0,flags 4
07-28 12:52:13.861: D/libc(25884): [NET] getaddrinfo-, SUCCESS
07-28 12:52:13.871: I/global(25884): call createSocket() return a new socket.
07-28 12:52:13.871: D/libc(25884): [NET] getaddrinfo+,hn 13(0x3139322e313638),sn(),family 0,flags 4
07-28 12:52:13.871: D/libc(25884): [NET] getaddrinfo-, SUCCESS 展开
7个回答
展开全部
android客户端和java服务端之间可以用socket来传输图片。
服务器端代码:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class Server02 {
public static void main(String[] args) {
try {
ServerSocket server = new ServerSocket(40000);
Socket socket = server.accept();
DataInputStream dos = new DataInputStream(socket.getInputStream());
int len = dos.available();
System.out.println("len = "+len);
byte[] data = new byte[len];
dos.read(data);
System.out.println("data = "+data);
dos.close();
socket.close();
server.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
客户端代码:
[java] view plaincopy
imageView02 = (ImageView)findViewById(R.id.image02);
button02 = (Button)findViewById(R.id.Button02);
button02.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
Socket socket;
try {
socket = new Socket("192.168.1.203",40000);
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.qt);
imageView02.setImageBitmap(bitmap);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//读取图片到ByteArrayOutputStream
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] bytes = baos.toByteArray();
out.write(bytes);
System.out.println("bytes--->"+bytes);
out.close();
socket.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
服务器端代码:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class Server02 {
public static void main(String[] args) {
try {
ServerSocket server = new ServerSocket(40000);
Socket socket = server.accept();
DataInputStream dos = new DataInputStream(socket.getInputStream());
int len = dos.available();
System.out.println("len = "+len);
byte[] data = new byte[len];
dos.read(data);
System.out.println("data = "+data);
dos.close();
socket.close();
server.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
客户端代码:
[java] view plaincopy
imageView02 = (ImageView)findViewById(R.id.image02);
button02 = (Button)findViewById(R.id.Button02);
button02.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
Socket socket;
try {
socket = new Socket("192.168.1.203",40000);
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.qt);
imageView02.setImageBitmap(bitmap);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//读取图片到ByteArrayOutputStream
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] bytes = baos.toByteArray();
out.write(bytes);
System.out.println("bytes--->"+bytes);
out.close();
socket.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
展开全部
服务端只要提供给客户端 图片下载的请求地址就行了,
你把图片都放在一个服务器某目录下,做一个请求的URl
你把图片都放在一个服务器某目录下,做一个请求的URl
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
本地获取服务端的话,是服务端返回一个url,然后本地加载网络图片bitmap流,然后加载到imageview里,服务端的话,就是本地图片上传了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
上传与下载
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |