下载图像并将其加载到listitem中的imageview中
我是新来的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);
你无法直接在ImageView中设置图像。你必须到图像转换成位图,然后设置
public static Bitmap getBitmapFromURL(String src) {
try {
Log.e("src",src);
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
Log.e("Bitmap","returned");
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
Log.e("Exception",e.getMessage());
return null;
}
}
然后设置ImageView的:
imageView.setImageBitmap(getBitmapFromURL(url));
试试这个,希望它可以帮助你
使用piccasso。
要使用piccasso增加其添加
compile 'com.squareup.picasso:picasso:2.5.2'
到您的应用水平gradle这个。而 使用这种
Picasso.with(context).load("http://previews.123rf.com/images/faysalfarhan/faysalfarhan1402/faysalfarhan140200008/25989999-Back-left-arrow-icon-glossy-purple-button-Stock-Photo.jpg").into(imageview);
或者你getCount将()方法返回0
。 (或添加它)将其更改为
@Override
public int getCount() {
return img.length;
}
我使用这个,但没有图像显示 –
我不明白什么是计数?! –
@ShoroukAdel它是一个在适配器覆盖的方法 –
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
考虑添加一些描述和评论给你的答案。 –
使用滑翔
依赖滑翔:编译 'com.github.bumptech.glide:滑翔:3.7.0'
,并添加代码
ImageView imageView = (ImageView) findViewById(R.id.my_image_view);
Glide.with(this).load("https://s3-us-west-1.amazonaws.com/powr/defaults/image-slider2.jpg").into(imageView);
我在listitem中使用了这个但没有图像显示在imageview中。尽管我用来显示列表中的图像,但它工作,但在listitem里面,它没有 –
通过:[https://futurestud.io/tutorials/glide- listadapter-列表视图-gridview的] –
您应该使用picasso或Glide来缓存和加载图像。 – Piyush
http://stackoverflow.com/a/9288544/7320259检查这个 –
只需传递你在DownloadImageTask中首次声明的imageView,如下所示:'DownloadImageTask download = new DownloadImageTask(imageView);' –