I have a button on my app that when I click (takes me to the picture gallery with an intent for result). When I select the picture, I get the result back fine, and then start transfering the image using a stand alone method.
The problem is that the UI gets stuck at the gallery screen and then the screen goes dark, until the transfer is complete… I try to update the UI using run on ui, but it doesn’t work…
while ((bytesRead = bis.read(buf, 0, buf.length)) != -1)
{
BT_data_out.write(buf, 0, bytesRead);
if(fill++ > 2) {BT_data_out.flush(); fill = 0;};
prgrss += bytesRead;
final String done = Integer.toString(prgrss);
final String total = Integer.toString(fileL);
if(update_rate > (50000/bufSize) ) {
update_rate = 0;
runOnUiThread(new Runnable()
{
@Override
public void run()
{
mTextMISC.setText(done+"/"+total+" B");
}
});
}
update_rate++;
}
Once the transfer is complete I can see the final effect of the UI thread, but all the intermediatary updates are never seen.
You cannot natively make updates to the main UI thread from a secondary thread. In order to do this you have to attach a Handler to the main UI thread and post messages to it from the secondary. Alternatively you can use an AsyncTask which is Androids implementation and makes using threads and handlers that much easier.