gpt4 book ai didi

java - 使用 AsyncTask 加载位图图像

转载 作者:太空狗 更新时间:2023-10-29 16:22:13 26 4
gpt4 key购买 nike

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

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 must be called,然后是逻辑 looper.quit()

但是,当我添加 Looper.prepare() 时,我似乎中断了程序,并且没有要调用的 looper.quit()。

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

EDIT:

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

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

最佳答案

问题是您正在尝试从非 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();

关于java - 使用 AsyncTask 加载位图图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11585226/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com