I am writing this application where I get live images from a tcp connection, and I need to display them on ImageViev.
What I'm doing causes asynthesis inside the button. but it seems to create a series of background threads.
this is the button click event code
btnLive.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try
{
String name = ((Button)v).getText().toString();
if(name.equalsIgnoreCase("Live"))
{
DataOutputStream dos;
DataInputStream dis;
String reply;
if(config.conn.isConnected())
{
dos = new DataOutputStream(config.conn.getOutputStream());
dos.writeBytes("STREAM-LIVE-IMAGES");
dos.flush();
in = config.conn.getInputStream();
while (true)
{
new myTask().execute(in);
}
}
}
}
catch(Exception ex)
{
Log.d("Live Button ", "Exception " + ex.getMessage() );
}
}
});
and this code for asyncTask
class myTask extends AsyncTask<InputStream, Integer, Bitmap> {
protected Bitmap doInBackground(InputStream...in)
{
Bitmap bmp = null;
try
{
byte[] rcvPacket = ReadJpegBinaryAndRemoveDelimiter(in[0]);
bmp = BitmapFactory.decodeByteArray(rcvPacket, 0, rcvPacket.length);
Log.d("Live Image Streaming ", "Recieved Images: " + rcvPacket.length + " " + bmp);
} catch (Exception e) {
e.printStackTrace();
}
return bmp;
}
source
share