Scale a bitmap and upload to server, ui thread blocking
<p>I'm working on an Android app where I need to upload an image to a server.</p>

<p>Before I upload the image, I'm scaling it to a max width/height of 500 with Bitmap.createScaledBitmap()</p>

<p>The problem I have is that the UI thread get's stuck even though I do both steps in a background thread.</p>

<p>My code looks like this:</p>

<pre><code>dialog = ProgressDialog.show(this, "", "Loading...", true);
// upload the image to the server
Thread t = new Thread() {
    @Override
    public void run() {
        bmp = BitmapHelper.scaleBmp(bmp, 500);//bmp is a private class Bitmap
        try {
            HttpResponse response = Helper.uploadBitmap(bmp);
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                response.getEntity().getContent(), "UTF-8"));
            StringBuilder builder = new StringBuilder();
            for (String line = null; (line = reader.readLine()) != null;) {
                builder.append(line).append("\n");
            }
            finalResult = new JSONObject(builder.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        handler.post(new Runnable() {
            @Override
            public void run() {
                handleData();
            }
        });
    }
};
t.run();
</code></pre>

<p>Any help on how to do this correctly would be really appreciated!</p>

<p>EDIT: adding handleData() function</p>

<pre><code>public void handleData() {
    take.setVisibility(View.VISIBLE);
    select.setVisibility(View.VISIBLE);

    process.setVisibility(View.GONE);
    select_new.setVisibility(View.GONE);

    message = "An error occured! Please try again.";
    boolean success = false;
    try {
        success = finalResult.getBoolean("success");
        url = "http://url";
        url += finalResult.getString("path");
        thumbnail = finalResult.getString("thumbnail");
    } catch (Exception e) {
        e.printStackTrace();
        Log.e(TAG, e.getMessage());
    }
    if (success) {
        message = "Picture was uploaded successfuly!";
    }
    dialog.dismiss();
    Config.toast(this, message);

    Thread t = new Thread() {
        @Override
        public void run() {
            final Bitmap b = HttpHelper.getBitmapFromURL(thumbnail);
            preview_image.post(new Runnable() {
                @Override
                public void run() {
                    preview_image.setImageBitmap(b);
                    preview_image.setVisibility(View.VISIBLE);
                    preview_image.setOnClickListener(context);
                }
            });
        }
    };
    t.run();
}
</code></pre>
