user4800039 user4800039 - 1 month ago 13
Android Question

send an image from java program to android program using sockets in eclipse IDE

I want to send a jpeg image file from java program client to android program server using TCP sockets in eclipse IDE.
here is my android server code:

package com.example.ando;

import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

import android.support.v7.app.ActionBarActivity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;


public class MainActivity extends ActionBarActivity{

ImageView ve;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ve=(ImageView)findViewById(R.id.picture);

}
public void gotit(View vw)
{
byte[] mybytearray = new byte[310000];
int bytesRead=0;
int current = 0;

ServerSocket serverSocket;
Socket servers;


try {
serverSocket = new ServerSocket(9000);
servers = serverSocket.accept();
servers.setSoTimeout(10000);
File myDir = new File("/mnt/sdcard/saved_images");

if (!myDir.exists()){
myDir.mkdir();
}else{
Log.d("ServerActivity","Folder Already created" );
}

String fpath = "/image0001.jpg";
File file = new File (myDir, fpath);
if (file.exists ()) file.delete ();


InputStream is = servers.getInputStream();
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = is.read(mybytearray,0,mybytearray.length);
current = bytesRead;

do {
bytesRead = is.read(mybytearray, current, (mybytearray.length-current));
if(bytesRead >= 0) current += bytesRead;
} while(bytesRead > -1);

ByteArrayInputStream inputStream = new ByteArrayInputStream(mybytearray);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
ve = new ImageView(this);
ve.setImageBitmap(bitmap);

bos.write(mybytearray, 0 , current);

Log.d("ServerActivity","Reconstructing Image from array");

bos.flush();
bos.close();
fos.flush();
fos.close();
is.close();
servers.close();
serverSocket.close();
} catch (Exception e) {
e.printStackTrace();
}



}


}


My xml file:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.ando.MainActivity" >

<ImageView
android:id="@+id/picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/abc_btn_check_material" />

<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/picture"
android:layout_below="@+id/picture"
android:layout_marginTop="90dp"
android:text="Button"
android:onClick="gotit" />

</RelativeLayout>


My Manifest file has following permissions:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ando"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>


My java client code is:

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.*;
import java.io.*;
import java.awt.Toolkit;
import java.awt.Robot;
import java.awt.Rectangle;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import java.net.*;

public class ConvertImage {

public static void main(String[] args) throws FileNotFoundException, IOException {
DataOutputStream dos;
PrintWriter out;
ByteArrayOutputStream baos;
BufferedImage buffimg;
BufferedImage image;

byte[] imageInByte;
try
{
Socket clients=new Socket("10.0.0.2",9000);
Robot robot=new Robot();

buffimg = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
image = buffimg;


baos = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", baos);
baos.flush();
imageInByte = baos.toByteArray();
baos.close();

dos = new DataOutputStream(clients.getOutputStream());
out = new PrintWriter(clients.getOutputStream());

out.println("!SCREENDATA!");
out.flush();
dos.writeInt(baos.toByteArray().length);
dos.write(baos.toByteArray());
dos.flush();
}catch(Exception e)
{
e.printStackTrace();
}
}

}


In java code i have this problem:

C:\Users\ABTILAK\Desktop\dos pracs>javac ConvertImage.java

C:\Users\ABTILAK\Desktop\dos pracs>java ConvertImage
java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at ConvertImage.main(ConvertImage.java:39)


I may be facing other code problems as well.
Anybody plz. help and thank you in advance...

Answer

It is a late reply but i got this code working by placing the android code into async task along with modifications and then i am passing the image from java program into android by converting into bytes. You can also use executor service rather than simple threads in the data transfer.