Reputation: 169
how i can receive an image and display it with tcp socket with an android device? i tried something but don't work. I'm searching for some example to send from pc an image or file and save it on the phone (android,java)
Upvotes: 0
Views: 2320
Reputation: 3304
I use something like that to download an image from a url:
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ImageView;
public class AsyncDownloadImage extends AsyncTask<ImageView, Void, Bitmap> {
private static final String TAG = "AsyncDownloadImage";
ImageView imageView = null;
@Override
protected Bitmap doInBackground(ImageView... imageViews) {
this.imageView = imageViews[0];
return DownloadImage((String) imageView.getTag());
}
@Override
protected void onPostExecute(Bitmap result) {
if (result != null)
imageView.setImageBitmap(result);
}
private InputStream OpenHttpConnection(String urlString) throws IOException {
InputStream in = null;
int response = -1;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
if (!(conn instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");
try {
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
} catch (Exception ex) {
throw new IOException("Error connecting");
}
return in;
}
private Bitmap DownloadImage(String URL) {
Bitmap bitmap = null;
InputStream in = null;
try {
in = OpenHttpConnection(URL);
bitmap = BitmapFactory.decodeStream(in);
if (in != null)
in.close();
} catch (IOException e1) {
Log.e(TAG, "Error in downloading image");
e1.printStackTrace();
}
return bitmap;
}
}
The way I use it is to set in the tag of the imageview, the url of the image I want to download and pass as a parameter the ImageView. Eg.
ImageView iv.setTag("http://www.example.com/image.png");
new AsyncDownloadImage().execute(iv);
In case you want to download it using a socket you can open a socket connection like:
Socket socket = new Socket(ip, port);
InputStream inputStream = socket.getInputStream();
Upvotes: 1