InputStream在Android上冻结UI

时间:2013-08-29 17:07:09

标签: java android android-asynctask inputstream

我正在下载背景图片以获取列表。我注意到即使使用AsycTask,滚动也不顺畅。经过几个小时的调试,我得出结论InputStream是有罪的。

doInBackground应该在非ui线程上工作,为什么它会影响UI? 如果我注释掉InputStream代码,滚动就会很顺利。

我连续有3张图片,所以通常会同时加载3张图片。

class BitmapWorkerTask extends AsyncTask<String, Void, Bitmap> {
    final WeakReference<ImageView> imageViewReference;

    public BitmapWorkerTask(ImageView imageView) {
        // Use a WeakReference to ensure the ImageView can be garbage collected
        imageViewReference = new WeakReference<ImageView>(imageView);
    }


    // Decode image in background.
    @Override
    protected Bitmap doInBackground(String... params) {

        InputStream is = null;

         try {
             is = (InputStream) new URL(params[0]).getContent();
         } catch (MalformedURLException e) {
            e.printStackTrace();
         } catch (IOException e) {
            e.printStackTrace();
         }


        return null;

    }


    // Once complete, see if ImageView is still around and set bitmap.
    @Override
    protected void onPostExecute(Bitmap bitmap) {
        // REMOVED ON PURPOSE TO CONFIRM THAT INPUTSTREAM IS GUILTY
    }

}

0 个答案:

没有答案