使用 AsyncTask 加载位图图像

Use AsyncTask to Load Bitmap Images(使用 AsyncTask 加载位图图像)
本文介绍了使用 AsyncTask 加载位图图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在后台加载图像,因为有人在使用我的应用程序.我写的逻辑是这样的:

I am trying to load an image in the background as someone works through my app. The logic I wrote is this:

public class ImageLoader extends AsyncTask <Context, Void, Bitmap>{

    private String URL;
    private int type;

    ImageLoader(String Url, int Type)
    {
        URL = Url;
        type = Type;
    }

    @Override
    protected Bitmap doInBackground(Context... arg0) {

        AssetManager assetMgr = arg0[0].getAssets();
        Bitmap bitmap = null;
        try {
            bitmap = BitmapFactory.decodeStream(assetMgr.open(URL));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bitmap;
    }

    @Override
    protected void onPostExecute( Bitmap result )  {
          super.onPostExecute(result);

          if (type == 1)
              Inst1 = result;
          else if (type == 2)
              Inst2 = result;
          else if (type == 3)
              Inst3 = result;
    }
}

但是,当我尝试像这样开始一个新线程时:

However when I try to start a new thread like this:

task = new ImageLoader("Instructions_2.png", 3);
task.execute(gameContext);

但在程序中我得到错误 Looper.prepare must be called, 其次是逻辑 looper.quit()

But within the program I get the error Looper.prepare must be called, followed by the logic looper.quit()

但是我在添加Looper.prepare()的时候好像破坏了程序,而且没有looper.quit()可以调用.

However I seem to break the program when I add Looper.prepare(), and there is no looper.quit() to call.

我是否正确地创建了任务?

Am I creating the task correctly?

这是我尝试运行时的错误日志:

This is the error log from when I try to run:

task = new ImageLoader(gameContext, "Instructions_3.png", 3);

我有一个 switch case 语句,我将图像加载程序声明放在了外面.基本上我的代码是:

I have a switch case statement that I put the image loader declaration outside of. Essentially my code is:

ImageLoader task;
switch(foo)
{
    case 0:
       ...
       task = new ImageLoader(gameContext, "Instructions_0.png", 3);
       task.execute();
       break;
    case 1:
       ...
       task = new ImageLoader(gameContext, "Instructions_1.png", 3));
       task.execute();
       break;
    ...
}

以及错误日志(每次我点击task = new ImageLoader(...); 行时都会发生错误

And the error log (error occurs every time I hit the task = new ImageLoader(...); line

07-20 14:23:34.276: E/AndroidRuntime(16741): FATAL EXCEPTION: Thread-10
07-20 14:23:34.276: E/AndroidRuntime(16741): java.lang.ExceptionInInitializerError
07-20 14:23:34.276: E/AndroidRuntime(16741):    at com.petronicarts.stormthecastle.MainGamePanel.update(MainGamePanel.java:2578)
07-20 14:23:34.276: E/AndroidRuntime(16741):    at com.petronicarts.stormthecastle.MainThread.run(MainThread.java:63)
07-20 14:23:34.276: E/AndroidRuntime(16741): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
07-20 14:23:34.276: E/AndroidRuntime(16741):    at android.os.Handler.<init>(Handler.java:121)
07-20 14:23:34.276: E/AndroidRuntime(16741):    at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:421)
07-20 14:23:34.276: E/AndroidRuntime(16741):    at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:421)
07-20 14:23:34.276: E/AndroidRuntime(16741):    at android.os.AsyncTask.<clinit>(AsyncTask.java:152)
07-20 14:23:34.276: E/AndroidRuntime(16741):    ... 2 more

推荐答案

问题是您正试图从非 UI 线程访问和操作 UI 元素.如果您将 AsyncTask 更改如下,我相信您会没事的:

The problem is that you are trying to access and operate on UI elements from a non UI thread. If you change your AsyncTask as follows, i believe you will be ok:

public class ImageLoader extends AsyncTask <Void, Void, Bitmap>{

private String URL;
private int type;
private Context context;
private InputStream in;

ImageLoader(Context context, String Url, int Type)
{
    URL = Url;
    type = Type;
    ImageLoader.this.context = context;
}

@Override
protected void onPreExecute()
{
   AssetManager assetMgr = context.getAssets();

   try {

       in = assetMgr.open(URL);
   } catch (IOException e) {

       e.printStackTrace();
   }

}

@Override
protected Bitmap doInBackground(Void... arg0) {

    Bitmap bitmap = null;
    try {
        bitmap = BitmapFactory.decodeStream(in);
        in.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bitmap;
}

@Override
protected void onPostExecute( Bitmap result )  {

      if (type == 1)
          Inst1 = result;
      else if (type == 2)
          Inst2 = result;
      else if (type == 3)
          Inst3 = result;
}
}

还将您的 AyncTask 的调用更改为如下所示:

Also change the call of your AyncTask to something like this:

task = new ImageLoader(gameContext, "Instructions_2.png", 3);
task.execute();

这篇关于使用 AsyncTask 加载位图图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How can create a producer using Spring Cloud Kafka Stream 3.1(如何使用Spring Cloud Kafka Stream 3.1创建制片人)
Insert a position in a linked list Java(在链接列表中插入位置Java)
Did I write this constructor properly?(我是否正确地编写了这个构造函数?)
Head value set to null but tail value still gets displayed(Head值设置为空,但仍显示Tail值)
printing nodes from a singly-linked list(打印单链接列表中的节点)
Control namespace prefixes in web services?(控制Web服务中的命名空间前缀?)