Send an image via TCP in the Android application

I am trying to connect two android applications using TCP protocol.
The client has an imageView, when you press the button, it should send the image to the server, and After the server reads it, it will display the image.
But I cannot display the sent image in the server.
Can someone help me?

This is my server code, it gets the image and shows it is obvious

package com.example.simpleserver;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android .os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
import android.widget.TextView;< br />
public class SimpleServer extends Activity {
ServerSocket ss = null;
Thread myCommsThread = null;
protected static final int MSG_ID = 0x1337;
public static final int SERVERPORT = 6000;
private Bitmap bitmap;

@Override
public void onCreate(Bundle savedInstanceState) {
super. onCreate(savedInstanceState);
setContentView(R.layout.activity_simple_server);
TextView tv = (TextView) findViewById(R.id.textView01);
tv.setText("Nothing from client yet ");
this.myCommsThread = new Thread(new CommsThread());
this.myCommsThread.start();
}

@Override
protected void onStop() {
super.onStop();
try {
// make sure you close the socket upon exiting
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}

Handler myUpdateHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_ID:
ImageView tv = (ImageView) findViewById(R.id.imageViewServer);
tv.setImageBitmap (bitmap);
break;
default:
break;
}
super.handleMessage(msg);
}
};
class CommsThread implements Runnable {
public void run() {
Socket s = null;
try {
ss = new ServerSocket(SERVERPORT );
} catch (IOException e) {
e. printStackTrace();
}
while (!Thread.currentThread().isInterrupted()) {
Message m = new Message();
m.what = MSG_ID;< br />
try {
if (s == null)
s = ss.accept();
InputStream in = s.getInputStream();
DataInputStream dis = new DataInputStream(in);

int len ​​= dis.readInt();
byte[] data = new byte[len];
if (len> 0) {
dis.readFull y(data);
}
bitmap = BitmapFactory.decodeByteArray(data, 0, data .length);
myUpdateHandler.sendMessage(m);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

This is my customer code, It conveys the image

package com.example.simpleclient;

import java.io.DataOutputStream;
import java.io.IOException ;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

public class SimpleClient extends Activity {
< br /> private Socket socket;

private static final int SERVERPORT = 5000;
private static final String SERVER_IP = "10.0.2.2";

@Override< br /> public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple_client);
new Thread(new ClientThread()).start( );
}

public void onClick(View view) {
try {;
getBytes();

} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e .printStackTrace();
}
}
public void getBytes() throws IOException{
ImageView iv=(ImageView)findViewById(R.id.imageView1);
//convert the image to bitmap to be send in the intent
Bitmap bmp=((BitmapDrawable)iv.getDrawable()).getBitmap();
int bytes = bmp.getByteCount();
ByteBuffer buffer = ByteBuffer.allocate(bytes); //Create a new buffer
bmp.copyPixelsToBuffer(buffer); //Move the byte data to the buffer

byte[] array = buffer.array();
int start=0;
int len=array.length;
if (len <0)
throw new IllegalArgumentException("Negative length not allowed ");
if (start <0 || start >= array.length)
throw new IndexOutOfBoundsException("Out of bounds: "+ start);

OutputStream out = socket.getOutputStream();
DataOutputStream dos = new DataOutputStream(out);

dos.writeInt(len);
if (len> 0) {
dos .write(array, start, len);
}

}

class ClientThread implements Runnable {

@Override
public void run() {

try {
InetAddress serverAddr = InetAddress.getByName(SERVER_IP);

socket = new Socket(serverAddr, SERVERPORT);

} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}

}

}
}

Try the following code to Your image is placed in a byte array:

ImageView iv=(ImageView)findViewById(R.id.imageView);
Bitmap bmp= ((BitmapDrawable)iv.getDrawable()).getBitmap();

ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.Com pressFormat.JPEG, 100, baos);
byte array [] = baos.toByteArray();

In addition, I must note that you execute getBytes() on the main thread. This only applies to Older Android version. It is best to put it in your thread.

I am trying to connect two android applications using TCP protocol.
Client There is an imageView, when you press the button, it should send the image to the server, and after the server reads it, it will display the image.
But I can’t display the sent image in the server.
Someone Can you help me?

This is my server code, it gets the image and shows it is obvious

package com.example.simpleserver;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android .os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
import android.widget.TextView;< br />
public class SimpleServer extends Activity {
ServerSocket ss = null;
Thread myCommsThread = null;
protected static final int MSG_ID = 0x1337;
public static final int SERVERPORT = 6000;
private Bitmap bitmap;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onC reate(savedInstanceState);
setContentView(R.layout.activity_simple_server);
TextView tv = (TextView) findViewById(R.id.textView01);
tv.setText("Nothing from client yet ");
this.myCommsThread = new Thread(new CommsThread());
this.myCommsThread.start();
}

@Override
protected void onStop() {
super.onStop();
try {
// make sure you close the socket upon exiting
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}

Handler myUpdateHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_ID:
ImageView tv = (ImageView) findViewById(R.id.imageViewServer);
tv.setImageBitmap (bitmap);
break;
default:
break;
}
super.handleMessage(msg);
}
};
class CommsThread implements Runnable {
public void run() {
Socket s = null;
try {
ss = new ServerSocket(SERVERPORT );
} catch (IOException e) {
e.printStackTrace( );
}
while (!Thread.currentThread().isInterrupted()) {
Message m = new Message();
m.what = MSG_ID;

try {
if (s == null)
s = ss.accept();
InputStream in = s.getInputStream();
DataInputStream dis = new DataInputStream(in);

int len ​​= dis.readInt();
byte[] data = new byte[len];
if (len> 0) {< br /> dis.readFully(d ata);
}
bitmap = BitmapFactory.decodeByteArray(data, 0, data .length);
myUpdateHandler.sendMessage(m);
} catch (IOException e) {< br /> e.printStackTrace();
}
}
}
}
}

This is my client code, it conveys Image

package com.example.simpleclient;

import java.io.DataOutputStream;
import java.io.IOException;< br /> import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java. nio.ByteBuffer;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android. os.Bundle;
import android.view.View;
import android.widget.ImageView;

public class SimpleClient extends Activity {

private Socket socket;

private static final int SERVERPORT = 5000;
private static final String SERVER_IP = "10.0.2.2";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple_client);
new Thread(new ClientThread()).start();< br /> }

public void onClick(View view) {
try {;
getBytes();

} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace( );
}
}
public void getBytes() throws IOException{
ImageView iv=(ImageView)findViewById(R.id.imageView1);
//convert the image to bitmap to be send in the intent
Bitmap bmp=((BitmapDrawable)iv.getDrawable()).getBitmap();
int bytes = bmp.getByteCount();
ByteBuffer buffer = ByteBuffer.allocate(bytes); //Create a new buffer
bmp.copyPixelsToBuffer(buffer); //Move the byte data to the buffer

byte[] array = buffer.array();
int start=0;
int len=array.length;
if (len <0)
throw new IllegalArgumentException("Negative length not allowed ");
if (start <0 || start >= array.length)
throw new IndexOutOfBoundsException("Out of bounds: "+ start);

OutputStream out = socket.getOutputStream();
DataOutputStream dos = new DataOutputStream(out);

dos.writeInt(len);
if (len> 0) {
dos .write(array, start, len);
}

}

class ClientThread implements Runnable {

@Override
public void run() {

try {
InetAddress serverAddr = InetAddress.getByName(SERVER_IP);

socket = new Socket(serverAddr, SERVERPORT);

} catch ( UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}

}< br />
}
}

Try the following code to put your image in a byte array:

< /p>

ImageView iv=(ImageView)findViewById(R.id.imageView);
Bitmap bmp=((BitmapDrawable)iv.getDrawable()).getBitmap();

ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte array [] = bao s.toByteArray();

Also, I must note that you execute getBytes() on the main thread. This only works on older Android versions. It is better to put it in your thread. < /p>

Leave a Comment

Your email address will not be published.