我有这个代码,它从drawables文件夹中获取图像。我需要从URL获取图像。我想我可以使用Picasso
来做,我试过了,但我无法指出它。
我在MainActivity中有这段代码:
public Drawable LoadImageFromWebOperations(String url) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "image_name");
return d;
} catch (Exception e) {
return null;
}
}
private Drawable d1 = LoadImageFromWebOperations("http://uupload.ir/files/aud7_brickone.jpg");
private List<App> getApps() {
List<App> apps = new ArrayList<>();
apps.add(new App("Google+", d1, 4.6f));
apps.add(new App("Google+", d1, 4.6f));
apps.add(new App("Google+", d1, 4.6f));
apps.add(new App("Google+", d1, 4.6f));
apps.add(new App("Google+", d1, 4.6f));
apps.add(new App("Google+", d1, 4.6f));
apps.add(new App("Google+", d1, 4.6f));
return apps;
}
这是我的适配器:
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
App app = mApps.get(position);
holder.imageView.setImageResource(app.getDrawable());
holder.nameTextView.setText(app.getName());
holder.ratingTextView.setText(String.valueOf(app.getRating()));
}
和APP.JAVA
public class App {
private Drawable mDrawable;
private String mName;
private float mRating;
public App (String name, Drawable drawable, float rating){
mName = name;
mDrawable = drawable;
mRating = rating;
}
public float getRating (){return mRating;}
public Drawable getDrawable (){return mDrawable;}
public String getName (){return mName;}
}
我需要来自以下链接的图片:http://uupload.ir/files/aud7_brickone.jpg
我无法实现!
答案 0 :(得分:1)
你也可以使用滑行, 这是代码
Glide.with(Your context).load(your image url).into(holder.Your imageview);
代码
Glide.with(context).load(app.getDrawable()).into(holder.imageView);
答案 1 :(得分:1)
从网址下载图片有多种方法。检查下面的功能:
public static Drawable LoadImageFromWebOperations(String url) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "any_image_name");
return d;
} catch (Exception e) {
return null;
}
}
然后只需在ImageView
中显示已退回的drawable。
不要忘记在清单中添加互联网权限:
<uses-permission android:name="android.permission.INTERNET" />
您还可以使用此其他方法,结合AsyncTask或后台线程:
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String url) {
String urldisplay = url;
Bitmap mIcon = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
然后您可以通过以下方式在任何地方拨打电话:
new DownloadImageTask((ImageView) findViewById(R.id.imageView))
.execute("http://uupload.ir/files/aud7_brickone.jpg");
希望有所帮助:)
答案 2 :(得分:0)
您可以使用Glide
或Picasso
。但我更喜欢你Picasso
,因为Picasso
的响应速度比Glide
快。这是代码:
导入Picasso
库,它就像魅力一样:
Picasso.with(context).load("image url").into(imageview);
答案 3 :(得分:0)