Android Java Socket通信发图片

这篇博客展示了如何在Android应用中使用Socket通信来发送选取的图片到Java服务器。用户点击按钮从图库选择图片,然后将图片的字节流发送到指定IP和端口的服务器。代码包括Android端的图库选择、图片处理和Socket发送功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Server端建Java project 放在电脑端跑,代码如下

public class server {

public static class MulticlientServer extends Thread{
private Socket client;
boolean flag = false;
public MulticlientServer(Socket c){
this.client=c;
}
public void run(){
try{
DataInputStream dis = new DataInputStream(client.getInputStream());
FileImageOutputStream fos = new FileImageOutputStream(new File("F:\\new.jpg"));
int len;
byte[] inputBytes = new byte[1024*8];
while((len = dis.read(inputBytes,0,inputBytes.length))>0){
System.out.println("正在接收数据。。。"+len);
flag = true;
fos.write(inputBytes);
fos.flush();
}
System.out.println("接收完成");
fos.close();
dis.close();
client.close();
}catch(IOException ex){}finally{}
}

public static void main(String[] args) throws IOException{
@SuppressWarnings("resource")
ServerSocket server=new ServerSocket(7777);
System.out.println("等待socket连接。。。");
while(true){
MulticlientServer mc=new MulticlientServer(server.accept());
System.out.println("收到socket请求!");
mc.start();
}}}
}

Client端建Android 代码在手机上跑,socket通信部分差不多,多了Android调用系统图库功能

button1.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
} ); //调用系统图库,还要写调用的回调函数如下,效果是按button1出来图库选择界面,选择完图片后,会加载到自己app界面上,注意!!一定要加权限!
  <uses-permission 

        android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };

Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();

int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
picturePath = cursor.getString(columnIndex);
cursor.close();
imageView1=(ImageView)findViewById(R.id.imageView1);
Log.i("SocketAndroidClient",picturePath);
Bitmap bm = BitmapFactory.decodeFile(picturePath);
imageView1.setImageBitmap(bm);
}
}

//button2就是socket发送图片的部分

button2.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
try {
Socket client = new Socket("10.150.193.72",7777);
Log.i("SocketAndroidClient","successful!");
DataOutputStream dos = new DataOutputStream(client.getOutputStream());
/* Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sunflower);
imageView1.setImageBitmap(bitmap);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);//读取图片到ByteArrayOutputStream
byte[] bytes = baos.toByteArray(); */
File file = new File(picturePath);
FileInputStream fis = new FileInputStream(file);
byte[] sendBytes = new byte[1024*8];
int len;
while((len = fis.read(sendBytes, 0, sendBytes.length)) > 0){
dos.write(sendBytes, 0, len);
dos.flush();
}
dos.close();
fis.close();
client.close();

} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}} }); }


(我想知道怎么贴代码?贴的跟在Eclipse里面显示的一样,那种便于阅读的。我自己也经常在优快云找代码,所以也想回报下但还不太会玩。。。)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值