我是新來的android,我在一個應用程序上工作。我想下載一個圖像並將其顯示在列表項中。我爲此使用了許多功能。我的代碼無誤地運行,但圖像不顯示在我的代碼的一部分屏幕上。下載圖像並將其加載到listitem中的imageview中
public loader(Context context, String[] img) {
super(context, R.layout.item, img);
this.context = context;
this.img = img;
inflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// ImageView imgshow = (ImageView) findViewById(R.id.image1);
if (null == convertView) {
convertView = inflater.inflate(R.layout.item, parent, false);
}
ImageView imageView = (ImageView) convertView.findViewById(R.id.imageview);
//first method
// Glide.with(context)
// .load(img[position])
//.into(imageView);
//second method
DownloadImageTask download=new DownloadImageTask((ImageView) convertView.findViewById(R.id.imageview));
download.execute("http://previews.123rf.com/images/faysalfarhan/faysalfarhan1402/faysalfarhan140200008/25989999-Back-left-arrow-icon-glossy-purple-button-Stock-Photo.jpg");
//new DownloadImageTask((ImageView) convertView.findViewById(R.id.imageview))
//.execute(img[position]);
//third method
//Picasso.with(context).load(img[position]).into(imageView);
//Log.e("image_url","https://upload.wikimedia.org/wikipedia/commons/thumb/2/27/Square_200x200.svg/1024px-Square_200x200.svg.png");
return convertView;
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
TextView t;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = "http://previews.123rf.com/images/faysalfarhan/faysalfarhan1402/faysalfarhan140200008/25989999-Back-left-arrow-icon-glossy-purple-button-Stock-Photo.jpg";
Bitmap mIcon11 = null;
// Toast.makeText(mainpage.this ,urldisplay ,Toast.LENGTH_SHORT).show();
TextView t=(TextView)findViewById(R.id.texttt);
// t.setText(urldisplay);
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
}
我把用在我的名單(IMG是一個字符串我保存網址,並顯示它們的陣列,但對於測試我的裝載機類中一個圖像的URL替換)
String[] img = new String[1000];
loader im=new loader(mainpage.this,img);
lv.setAdapter(im);
您應該使用picasso或Glide來緩存和加載圖像。 – Piyush
http://stackoverflow.com/a/9288544/7320259檢查這個 –
只需傳遞你在DownloadImageTask中首次聲明的imageView,如下所示:'DownloadImageTask download = new DownloadImageTask(imageView);' –