How to send a bitmap using serversocket - java

I am making a multiplayer drawing application for android and I need to send the drawings that every user made to one player. I am using a server socket for this.
First thing I do is convert the Bitmap to a byte array, so I can send it to the Host with host.write(byteArray);
Bitmap bitmapImage = drawView.getBitmap();
byte[] byteArray = getByteArray(bitmapImage);
byteArrayLength = byteArray.length;
MainWifiActivity.SendReceive host = MainWifiActivity.sendReceiveHost;
if (host != null) {
host.write(byteArray);
}
The following code is my SendReceive class, which listenes to the inputStream and then starts a Handler, that is supposed to save the Bitmap to Internal Storage
public class SendReceive extends Thread {
private Socket socket;
private InputStream inputStream;
private OutputStream outputStream;
public SendReceive(Socket s) {
socket = s;
try {
inputStream = s.getInputStream();
outputStream = s.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void run() {
byte[] buffer = new byte[1024];
int bytes;
int filesize;
while (socket != null) {
try {
filesize = DrawingActivity.byteArrayLength;
if(buffer.length != filesize && filesize > 0){
buffer = new byte[filesize];
}
bytes = inputStream.read(buffer,0 ,buffer.length);
if (bytes > 0) {
Message mesg = handler.obtainMessage(IMAGE_MSG, bytes, -1, buffer);
mesg.sendToTarget();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
And the Handler:
Handler handler = new Handler(new Handler.Callback() {
#Override
public boolean handleMessage(Message msg) {
switch (msg.what) {
case IMAGE_MSG:
byte[] byteArray = (byte[]) msg.obj;
Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
saveBitmapToInternalStorage(bitmap);
}
return false;
}
});
In the saveBitmapToInternalStorage Method I get a java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference
private void saveBitmapToInternalStorage(Bitmap bmp) {
File directory = getApplicationContext().getDir("imageDir", Context.MODE_PRIVATE);
File myPath = new File(directory, UUID.randomUUID().toString() + ".png");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(myPath);
bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
} catch (Exception e) {
Log.d("HELLO", "MY ERROR: " + e);
} finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
I know that BitmapFactory.decodeByteArray returns the decoded bitmap, or null if the image could not be decoded.
But why could it not be decoded?

I think this piece is wrong
FileOutputStream fos = null;
try {
fos = new FileOutputStream(myPath);
bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
} catch (Exception e) {
It should be
FileOutputStream fos = new FileOutputStream(myPath);
try {
bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
} catch (Exception e) {

Related

App crashed when sending images through TCP

I'm creating a simple stream to send images taken from client's screen from client to server. For now I can receive the first image but then the app crashed unexpectedly. The idea is send the size and the image in byte array, the server receive that byte array and convert to image.
FromClient:
public void run() {
image = new BufferedImage(NORM_PRIORITY, MIN_PRIORITY, MAX_PRIORITY);
while(continueLoop) {
//send captured screen
image = robot.createScreenCapture(rectangle);
try {
//Initiate the stream
OutputStream out = clientSocket.getOutputStream();
DataOutputStream dos = new DataOutputStream(out);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", baos);
// store the size of each image
byte[] size = ByteBuffer.allocate(4).putInt(baos.size()).array();
dos.write(size);
dos.write(baos.toByteArray(), 0, baos.toByteArray().length);
dos.flush();
} catch(IOException e) {
e.printStackTrace();
e.printStackTrace();
continueLoop = false;
}
try {
Thread.sleep(30);
} catch(InterruptedException e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
ToServer:
public void run() {
boolean continueLoop = true;
try {
drawGUI();
// Initiate the stream
InputStream is = clientSocket.getInputStream();
DataInputStream configGraphicStream = new DataInputStream(is);
BufferedImage image = null;
while(continueLoop) {
//receive the size of image and convert to type int
byte[] sizeInByte = new byte[64];
configGraphicStream.read(sizeInByte);
int length = ByteBuffer.wrap(sizeInByte).asIntBuffer().get();
try {
// Get images
byte[] img = new byte[length];
configGraphicStream.readFully(img, 0, img.length);
image = ImageIO.read(new ByteArrayInputStream(img));
}
catch (Exception e) {
System.out.println(e.getMessage());
}
//draw images
if( image != null)
{
Graphics graphics = clientPanel.getGraphics();
graphics.drawImage(image, 0, 0, clientPanel.getWidth(), clientPanel.getHeight(), clientPanel);
}
System.out.println("Receiving image");
Thread.sleep(30);
}
} catch (Exception e) {
e.printStackTrace();
}
}
Please help me solve this problem.

Catch 404 error from BufferedInputStream

I have a function for html page download.
Here is the code:
public class pageDownload {
public static void down(final String filename, final String urlString)
throws MalformedURLException, IOException {
BufferedInputStream in = null;
FileOutputStream fout = null;
try {
in = new BufferedInputStream(new URL(urlString).openStream());
fout = new FileOutputStream(new File(filename));
final byte data[] = new byte[1024];
int count;
while ((count = in.read(data, 0, 1024)) != -1) {
fout.write(data, 0, count);
}
} catch (IOException e) {
System.err.println("Caught IOException: " + e.getMessage());
} catch (IndexOutOfBoundsException e) {
System.err.println("IndexOutOfBoundsException: " + e.getMessage());
}
in.close();
fout.close();
}
}
Works ok, problem appears when i try to download a page that not exist. I can't figure out how to handle 404 error in this case.
Has anyone some idea?
Do you mean something like this? I added a finally to save close the Streams
public class pageDownload {
public static void down(final String filename, final String urlString)
throws MalformedURLException, IOException {
BufferedInputStream in = null;
FileOutputStream fout = null;
try {
in = new BufferedInputStream(new URL(urlString).openStream());
fout = new FileOutputStream(new File(filename));
final byte data[] = new byte[1024];
int count;
while ((count = in.read(data, 0, 1024)) != -1) {
fout.write(data, 0, count);
}
} catch(FileNotFoundException ex)
{
System.err.println("Caught 404: " + e.getMessage());
}
catch(IOException ex)
{
System.err.println("Caught IOException: " + e.getMessage());
}
catch(IndexOutOfBoundsException e)
{
System.err.println("IndexOutOfBoundsException: " + e.getMessage());
}
finally{
if(in != null)
try { in.close(); } catch ( IOException e ) { }
if(fout != null)
try { fout.close(); } catch ( IOException e ) { }
}
}
}
Your problem is you get a NullPointerException when you try to close the streams. You should anyway close them in a finally clause or use try with resources:
public static void down(final String filename, final String urlString)
throws IOException {
try (BufferedInputStream in = new BufferedInputStream(new URL(urlString)
.openStream());
FileOutputStream fout = new FileOutputStream(new File(filename))) {
final byte data[] = new byte[1024];
int count;
while ((count = in.read(data, 0, 1024)) != -1) {
fout.write(data, 0, count);
}
} catch (IOException e) {
System.err.println("Caught IOException: " + e.getMessage());
} catch (IndexOutOfBoundsException e) {
System.err.println("IndexOutOfBoundsException: " + e.getMessage());
}
}

Read form ZipDecryptInputStream and write it to OutputStream

public void decrypt(String inputFile, String password) {
ZipDecryptInputStream zipDecrypt = null;
try {
zipDecrypt = new ZipDecryptInputStream(new FileInputStream(
inputFile), password);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
File file = new File("outouttfile.tsv");
OutputStream fop = null;
try {
fop = new FileOutputStream(file);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
try {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = zipDecrypt.read(buffer)) != -1) {
fop.write(buffer, 0, bytesRead);
System.out.println("Written");
}
} catch (IOException e) {
e.printStackTrace();
}
}
My while loop becomes an infinite loop and does not stop reading the file even its read once. Any idea why?

Receiving multiple byte arrays from socket

I'm currently working on a remote desktop app. The server part runs on Android device and the client one on PC/Raspberry PI. I've encountered a problem with receiving my bitmaps (screenshots from Android, send as byte arrays).
The Android Server code goes like this:
//os = new ObjectOutputStream(s.getOutputStream());
public void run() {
while(true){
try {
View v1 = getWindow().getDecorView().getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
int quality = 100;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, stream);
byte[] byteArray = stream.toByteArray();
if(byteArray.length>0) {
os.writeInt(byteArray.length);
os.write(byteArray, 0, byteArray.length);
os.flush();
}
} catch (IOException e) {
e.printStackTrace();
}}}
And the Receiver App on PC like this:
//ois = new ObjectInputStream(s.getInputStream());
public void run() {
while(true){
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
int size = ois.readInt();
System.out.println(size);
byte[] data = new byte[size];
int length = 0;
while ((length = ois.read(data,0,size)) != -1) {
out.write(data, 0, size);
out.flush();
}
byte[] bytePicture = out.toByteArray();
out.close();
BufferedImage buffImage = byteArrayToImage(bytePicture);
ImageIcon imgIcon = new ImageIcon(buffImage);
l.setIcon(imgIcon);
} catch (IOException e) {
}}}
byteArrayToImage
public static BufferedImage byteArrayToImage(byte[] bytes) {
BufferedImage bufferedImage = null;
try {
InputStream inputStream = new ByteArrayInputStream(bytes);
bufferedImage = ImageIO.read(inputStream);
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
return bufferedImage;
}
The problem is that in the receiver app I get Java OutOfMemoryException because I cannot differ between incoming screenshots. I really cannot figure out how to read exactly the size of a bitmap (as byte array) then display it and read another and another one. Any help will be appreciated. Thanks in advance.

Java sockets file sending

I am writing Client/Server using java sockets. There is my code:
SERVER:
public void sendFile(File file) {
BufferedOutputStreambufferedOutputStream = new BufferedOutputStream(socket.getOutputStream());
int count;
FileInputStream in;
try {
in = new FileInputStream(file);
byte[] mybytearray = new byte[(int) file.length()];
while ((count = in.read(mybytearray)) > 0) {
bufferedOutputStream.write(mybytearray, 0, count);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
CLIENT:
public void downloadFile() {
BufferedInputStream bufferedInputStream = new BufferedInputStream(socket.getInputStream());
byte[] aByte = new byte[8192];
int count;
FileOutputStream in;
try {
in = new FileOutputStream("C://fis.txt");
while ((count = bufferedInputStream.read(aByte)) > 0) {
System.out.println(count); // <- nothing happens
in.write(aByte, 0, count);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Why bufferedInputStream in 2nd function is empty ?
BufferedOutputStream will not write data until the buffer is full. You need to flush the OutputStream:
bufferedOutputStream.flush();

Categories