场景:我必须从服务器下载图像并将其设置为动态创建按钮的背景。图像数量不断变化,因此我必须动态创建许多按钮。
到目前为止我已经完成了:我成功地使用AsyncTask下载了图像,并将它们设置为动态创建按钮的背景。如果没有互联网连接或由于某种原因未下载图像,则将每个按钮的背景设置为某个默认图像。
问题:手机上的一切工作都很好,即根据图像数量动态下载图像和创建按钮,并将图像设置为背景。现在,当我在Tablet上运行相同的程序时,它无法运行。按钮不是动态生成的。但是,如果我关闭互联网然后运行,就会发生奇迹。一切正常,即按钮是以默认图像为背景动态生成的。我无法理解为什么会这样。 AsyncTask有什么问题吗?任何帮助将不胜感激..
这是AsyncTask
public class GetsImages extends AsyncTask<String, Void, Bitmap> {
FileCache fileCache;
ProgressDialog pd;
String url;
GetImages task = null;
Context context;
private Bitmap image;
HomeDynamic activity;
AsyncImage responseImage = null;
public GetImages(String url, HomeDynamic activity) {
this.url = url;
// this.context = context;
this.responseImage = activity;
attach(activity);
fileCache = new FileCache(activity);
//pd = new ProgressDialog(activity);
}
public GetImages(Context context) {
this.context = context;
}
protected void onPreExecute() {
super.onPreExecute();
/*pd.setTitle("Processing");
pd.setMessage("Please wait...");
pd.setCancelable(false);
pd.setIndeterminate(true);
pd.show();*/
}
@Override
protected void onPostExecute(Bitmap result) {
/*if (pd.isShowing())
pd.dismiss();*/
// studentPic.setImageBitmap(result);
responseImage.processImage(result);
}
public void detach() {
activity = null;
}
public void attach(HomeDynamic activity) {
this.activity = activity;
}
public static void CopyStream(InputStream is, OutputStream os) {
final int buffer_size = 1024;
try {
byte[] bytes = new byte[buffer_size];
for (;;) {
int count = is.read(bytes, 0, buffer_size);
if (count == -1)
break;
os.write(bytes, 0, count);
}
} catch (Exception ex) {
}
}
@Override
protected Bitmap doInBackground(String... arg0) {
// TODO Auto-generated method stub
File f = fileCache.getFile(url);
// from SD cache
Bitmap b = decodeFile(f);
if (b != null)
return b;
// from web
try {
Bitmap bitmap = null;
URL imageUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) imageUrl
.openConnection();
conn.setConnectTimeout(15000);
conn.setReadTimeout(15000);
conn.setInstanceFollowRedirects(true);
InputStream is = conn.getInputStream();
OutputStream os = new FileOutputStream(f);
CopyStream(is, os);
os.close();
bitmap = decodeFile(f);
return bitmap;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
private Bitmap decodeFile(File f) {
try {
// decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
// Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE = 64;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE
|| height_tmp / 2 < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
// decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(new
FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {
}
return null;
}
}
答案 0 :(得分:0)
如果没有显示您执行的任何代码,则很难确定问题所在。如果您的活动涉及如此多的照片,我建议您使用Picasso库来处理图像。