Question

I've got a strange issue with my code which haven't been able to resolve during the last couple of days. Let me put it this way: I have an array of two threads which retrieve images from 2 separate streams and I am updating two imageviews on my screen with each stream. Therefore stream1 updates imageview1 and stream2 updates imageview2. However, when I do this, the streams keep swapping images time to time. That is, stream1 images go on imageview2 and vice versa. I've debugged the code to save images in two different folders depending on the thread indexes and to see the images that get saved aren't mixed up at all. My code is too long to post on this, but I'll try to narrow it down and put up the important bits. Oh, and I also added logs to see if they keep swapping but to see the code runs through perfectly and logs proves it.

UPDATE

Calling the two threads

if(stream.size() >= 1)
{
    try
    {
        splitScreenLiveThreads[0] = new Thread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                LiveImages(0);
            }
        });
        splitScreenLiveThreads[0].start();
    }catch(Exception ex){

    }
}
if(stream.size() >= 2)
{
    try
    {
        splitScreenLiveThreads[1] = new Thread(new Runnable() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                LiveImages(1);
            }
        });
        splitScreenLiveThreads[1].start();
    }
    catch(Exception ex)
    {
    }
}

Updating the UI

byte [] b = getImageBytes(thIndex);
final Bitmap bmp = latestFrames[thIndex];
    switch (thIndex)
    {                   
    case 0: 
    {
        imgView1.post(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                imgView1.setImageBitmap(bmp);
            }
        });
        //latestFrame.recycle();
        break;
    }

    case 1: 
    {
        imgView2.post(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                imgView2.setImageBitmap(bmp);
            }
        });
        //latestFrame.recycle();
        break;
    }
    }
Was it helpful?

Solution

Right guys I managed to fix my issue it was a bug in my code...just realized my height of stupidity and the time I loss trying to fix it. Basically I was trying to do some memory management by disposing a few bitmaps and among them there was one global bitmap variable which i was using as an intermediate variable, that was shared by the two threads and this was causing the issue.

OTHER TIPS

I pretty sure you should use Handler . (http://developer.android.com/reference/android/os/Handler.html)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top