简体   繁体   English

缩放位图并上传到服务器,ui线程阻塞

[英]Scale a bitmap and upload to server, ui thread blocking

I'm working on an Android app where I need to upload an image to a server. 我正在使用一个Android应用程序,需要将图像上传到服务器。

Before I upload the image, I'm scaling it to a max width/height of 500 with Bitmap.createScaledBitmap() 上传图片之前,我要使用Bitmap.createScaledBitmap()将图片缩放到最大宽度/高度500

The problem I have is that the UI thread get's stuck even though I do both steps in a background thread. 我遇到的问题是,即使我在后台线程中同时执行两个步骤,UI线程也会卡住。

My code looks like this: 我的代码如下所示:

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();

Any help on how to do this correctly would be really appreciated! 非常感谢您提供有关如何正确执行此操作的帮助!

EDIT: adding handleData() function 编辑:添加handleData()函数

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();
}

I think the problem is you are starting a new thread and then inside of it you're using Handler to post another runnable . 我认为问题在于您正在启动一个新thread ,然后在其中使用Handler来发布另一个runnable So, the Handler is associated with this new Thread and you cannot manipulate the UI thread from that thread . 因此, Handler程序与此新Thread关联,并且您无法从该thread操纵UI thread It seems to me that you really should be using an AsyncTask , which provides a convenience for switching from background threads to the UI thread very easily. 在我看来,您确实应该使用AsyncTask ,它提供了非常方便的从后台线程切换到UI线程的便利。

http://developer.android.com/reference/android/os/AsyncTask.html http://developer.android.com/reference/android/os/AsyncTask.html

You are not starting new threads. 您没有启动新线程。 You are creating thread objects but you never start new threads. 您正在创建线程对象,但从未启动新线程。 Instead, you just execute the method run in the current thread. 相反,您只需执行在当前线程中运行的方法即可。 For reference how to start a new thread see Defining and Starting a Thread . 有关如何启动新线程的参考,请参阅定义和启动线程

As @LuxuryMode says, AsyncTask is a very good alternative on Android for long running operations that should not block the ui thread. 正如@LuxuryMode所说,AsyncTask是Android上非常好的替代方案,用于长时间运行的操作,这些操作不应阻塞ui线程。

除了其他发布者所说的以外,运行线程是通过调用start()而不是run()来完成的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM