使用AsyncTask加载位图图像

时间:2012-07-20 18:46:09

标签: java android android-asynctask loader

我正在尝试在有人通过我的应用程序工作时在后台加载图像。我写的逻辑是:

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

然而,当我尝试开始这样的新线程时:

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

但是在程序中我得到错误Looper.prepare必须被调用,然后是逻辑looper.quit()

但是当我添加Looper.prepare()时,我似乎打破了程序,并且没有looper.quit()来调用。

我是否正确创建了任务?

  

编辑:

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

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

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

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

时都会出错
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

1 个答案:

答案 0 :(得分:6)

问题是您正在尝试从非UI线程访问和操作UI元素。如果您按如下方式更改AsyncTask,我相信您会好的:

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的来电更改为以下内容:

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