现在的位置: 首页 > 综合 > 正文

Android Java Socket通信发图片

2018年05月08日 ⁄ 综合 ⁄ 共 3008字 ⁄ 字号 评论关闭

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里面显示的一样,那种便于阅读的。我自己也经常在CSDN找代码,所以也想回报下但还不太会玩。。。)

抱歉!评论已关闭.