gpt4 book ai didi

java - android 延迟加载图库问题

转载 作者:行者123 更新时间:2023-12-01 15:47:40 28 4
gpt4 key购买 nike

我尝试在异步任务中异步下载一些图像,然后在 onPostExecute() 中将图像设置为图库适配器。我已经对要在 asyntask 中下载的图像进行了硬编码,因为我什至无法到达将图像传递给适配器的构造函数的部分:/不会引发任何错误,并且 postexecute Log() 不会将任何内容发布到 logcat

谁能告诉我哪里出错了?

公共(public)类 GallerythreadedtestActivity 扩展了 Activity {

private Gallery gallery;
private AddImgAdp adapter;
private Drawable[] image;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new downloadImages().execute();
}

public class AddImgAdp extends BaseAdapter {
int GalItemBg;
private Context cont;

// Adding images.
private Integer[] Imgid = {
R.drawable.icon, R.drawable.icon, R.drawable.icon, R.drawable.icon
};

public AddImgAdp(Context c) {
cont = c;
TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
GalItemBg = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);
typArray.recycle();
}

public int getCount() {
return Imgid.length;
}

public Object getItem(int position) {
return position;
}

public long getItemId(int position) {
return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
ImageView imgView = new ImageView(cont);

imgView.setImageDrawable(image[position]);
// Fixing width & height for image to display
imgView.setLayoutParams(new Gallery.LayoutParams(80, 70));
imgView.setScaleType(ImageView.ScaleType.FIT_XY);
imgView.setBackgroundResource(GalItemBg);

return imgView;
}
}

public class downloadImages extends AsyncTask<String, Void, Void> {

protected void onPreExecute(){
image=new Drawable[2];
adapter = new AddImgAdp(getApplicationContext());
gallery = (Gallery) findViewById(R.id.examplegallery);
}

protected void onProgressUpdate(Void... v){

}

protected Void doInBackground(final String... args) {


image[0] = ImageOperations(getApplicationContext(),"http://t3.gstatic.com/images?q=tbn:ANd9GcQidl6KX2jRWNeCA6jT_TjWG7NlI3aRiB_AcDsA9Y5owS2cr9G6","image.jpg");
image[1] = ImageOperations(getApplicationContext(),"http://t3.gstatic.com/images?q=tbn:ANd9GcQidl6KX2jRWNeCA6jT_TjWG7NlI3aRiB_AcDsA9Y5owS2cr9G6","image.jpg");

return null;

}

protected void onPostExecute(){
Log.d("onpost","");
gallery.setAdapter(adapter);
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
Toast.makeText(GallerythreadedtestActivity.this, "Position=" + position, Toast.LENGTH_SHORT).show();
}
});
}

}

private Drawable ImageOperations(Context ctx, String url, String saveFilename) {
try {
InputStream is = (InputStream) this.fetch(url);
Drawable d = Drawable.createFromStream(is, "src");
return d;
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}

public Object fetch(String address) throws MalformedURLException,IOException {
URL url = new URL(address);
Object content = url.getContent();
return content;
}

}

最佳答案

我可以给你一个建议,请也尝试一下这个作为补充解决方案,它更好地延迟加载图像:Lazy load of images in ListView 。如果您将 xml 中的 ListView 元素更改为图库 View ,我认为这将解决您的问题。

关于java - android 延迟加载图库问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6822117/

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