Android 从服务器下载图像并将其作为动态创建按钮的背景应用
场景:我必须从服务器下载图像,并将它们设置为动态创建按钮的背景。图像的数量一直在变化,所以我必须动态创建这么多的按钮 到目前为止,我已经完成了:我成功地使用AsyncTask下载了这些图像,并将它们设置为动态创建按钮的背景。如果没有internet连接或由于某种原因没有下载图像,则将每个按钮的背景设置为某个默认图像 问题:手机上的一切都很好,即下载图像,根据图像数量动态创建按钮,并将图像设置为背景。现在,当我在平板电脑上运行相同的程序时,它不起作用。按钮不是动态生成的。但如果我打开互联网连接,然后运行,奇迹就会发生。!一切正常,即按钮以默认图像作为背景动态生成。我不明白为什么会这样。!AsyncTask有什么问题吗?任何帮助都将不胜感激 这是异步任务Android 从服务器下载图像并将其作为动态创建按钮的背景应用,android,android-asynctask,android-button,Android,Android Asynctask,Android Button,场景:我必须从服务器下载图像,并将它们设置为动态创建按钮的背景。图像的数量一直在变化,所以我必须动态创建这么多的按钮 到目前为止,我已经完成了:我成功地使用AsyncTask下载了这些图像,并将它们设置为动态创建按钮的背景。如果没有internet连接或由于某种原因没有下载图像,则将每个按钮的背景设置为某个默认图像 问题:手机上的一切都很好,即下载图像,根据图像数量动态创建按钮,并将图像设置为背景。现在,当我在平板电脑上运行相同的程序时,它不起作用。按钮不是动态生成的。但如果我打开互联网连接,然
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;
}
}
公共类GetsImages扩展异步任务{
文件缓存文件缓存;
进展性帕金森病;
字符串url;
GetImages任务=null;
语境;
私有位图图像;
家庭动力活动;
AsyncImage responseImage=null;
公共GetImages(字符串url、HomeDynamic活动){
this.url=url;
//this.context=上下文;
this.responseImage=活动;
附加(活动);
fileCache=新的fileCache(活动);
//pd=新进度对话框(活动);
}
公共GetImages(上下文){
this.context=上下文;
}
受保护的void onPreExecute(){
super.onPreExecute();
/*pd.setTitle(“处理”);
设置消息(“请稍候…”);
pd.可设置可取消(假);
pd.SetUndeterminate(真);
pd.show()*/
}
@凌驾
受保护的void onPostExecute(位图结果){
/*if(pd.isShowing())
pd.解散()*/
//studentPic.setImageBitmap(结果);
responseImage.processImage(结果);
}
公共图书馆{
活动=空;
}
公共无效附加(HomeDynamic活动){
这个。活动=活动;
}
公共静态void CopyStream(InputStream是,OutputStream os){
最终整数缓冲区大小=1024;
试一试{
字节[]字节=新字节[缓冲区大小];
对于(;;){
int count=is.read(字节,0,缓冲区大小);
如果(计数==-1)
打破
写入(字节,0,计数);
}
}捕获(例外情况除外){
}
}
@凌驾
受保护位图doInBackground(字符串…arg0){
//TODO自动生成的方法存根
文件f=fileCache.getFile(url);
//从SD缓存
位图b=解码文件(f);
如果(b!=null)
返回b;
//从网络
试一试{
位图=空;
URL imageUrl=新URL(URL);
HttpURLConnection conn=(HttpURLConnection)imageUrl
.openConnection();
连接设置连接超时(15000);
连接设置读取超时(15000);
conn.setInstanceFollowRedirects(真);
InputStream is=conn.getInputStream();
OutputStream os=新文件OutputStream(f);
CopyStream(is、os);
os.close();
位图=解码文件(f);
返回位图;
}捕获(例外情况除外){
例如printStackTrace();
返回null;
}
}
私有位图解码文件(文件f){
试一试{
//解码图像大小
BitmapFactory.Options o=新的BitmapFactory.Options();
o、 inJustDecodeBounds=true;
解码流(新的FileInputStream(f),null,o);
//找到正确的刻度值。它应该是2的幂。
所需最终整型尺寸=64;
内部宽度=o.向外宽度,高度=o.向外高度;
int标度=1;
while(true){
如果(宽度\u tmp/2<要求的\u尺寸
||高度(tmp/2<所需尺寸)
打破
宽度_tmp/=2;
高度_tmp/=2;
比例*=2;
}
//用inSampleSize解码
BitmapFactory.Options o2=新的BitmapFactory.Options();
o2.inSampleSize=刻度;
返回BitmapFactory.decodeStream(新
FileInputStream(f),null,o2);
}catch(filenotfounde异常){
}
返回null;
}
}
如果不显示您所执行的任何代码,则很难找出问题所在。如果您的活动涉及到这么多照片,我建议使用毕加索图书馆,它将在处理图像方面做得非常出色。感谢所有阅读我的长篇问题的人。我刚刚添加了权限。现在它工作得很好。:)