【发布时间】:2013-09-23 07:15:04
【问题描述】:
我想使用 Url 在 ImageView 中设置图像,例如我有这个 url
但是没有设置url的选项
【问题讨论】:
-
你可以使用 ImageLoader 类,或者你可以参考一些如何从 url 获取图像的链接???
-
缺乏解决问题的努力。
我想使用 Url 在 ImageView 中设置图像,例如我有这个 url
但是没有设置url的选项
【问题讨论】:
编辑:
创建一个扩展AsyncTask的类
public class ImageLoadTask extends AsyncTask<Void, Void, Bitmap> {
private String url;
private ImageView imageView;
public ImageLoadTask(String url, ImageView imageView) {
this.url = url;
this.imageView = imageView;
}
@Override
protected Bitmap doInBackground(Void... params) {
try {
URL urlConnection = new URL(url);
HttpURLConnection connection = (HttpURLConnection) urlConnection
.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
imageView.setImageBitmap(result);
}
}
然后像new ImageLoadTask(url, imageView).execute();
这样称呼它
直接法:
使用此方法并将您的网址作为字符串传递。它返回一个位图。将位图设置为您的 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));
并且不要忘记 maifest 中的这个权限。
<uses-permission android:name="android.permission.INTERNET" />
注意:
尝试从另一个线程或 AsyncTask 调用此方法,因为我们正在执行网络操作。
【讨论】:
onCreate方法中添加这个代码,下面提到的代码StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy);
您也可以让 Square 的 Picasso 库完成繁重的工作:
Picasso
.get()
.load("http://...")
.into(imageView);
作为奖励,您可以获得缓存、转换等。
【讨论】:
试试:
URL newurl = new URL(photo_url_str);
mIcon_val = BitmapFactory.decodeStream(newurl.openConnection() .getInputStream());
profile_photo.setImageBitmap(mIcon_val);
更多来自
【讨论】:
使用Glide 库:
Glide.with(context)
.load(new File(url)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(imageView);
【讨论】:
您可以使用Picasso 或Glide。
Picasso.get()
.load(your_url)
.into(imageView);
Glide.with(context)
.load(your_url)
.into(imageView);
【讨论】:
使用Aquery库的简单方法,它有助于从url直接加载图像
AQuery aq=new AQuery(this); // intsialze aquery
aq.id(R.id.ImageView).image("http://www.vikispot.com/z/images/vikispot/android-w.png");
【讨论】:
试试 SimpleDraweeView 库
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/badge_image"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
现在你可以简单地做:
final Uri uri = Uri.parse(post.getImageUrl());
【讨论】:
如果您正在制作 RecyclerView 并使用适配器,那么对我有用的是:
@Override
public void onBindViewHolder(ADAPTERVIEWHOLDER holder, int position) {
MODEL model = LIST.get(position);
holder.TEXTVIEW.setText(service.getTitle());
holder.TEXTVIEW.setText(service.getDesc());
Context context = holder.IMAGEVIEW.getContext();
Picasso.with(context).load(model.getImage()).into(holder.IMAGEVIEW);
}
【讨论】:
使用最新版本的毕加索(撰写此答案时为 2.71828),with 方法已被弃用。
所以正确的方法是-
Picasso.get().load("https://<image-url>").into(imageView);
其中imageView 是您要将图像加载到其中的ImageView。
【讨论】:
对于单行程序员,
imageView.setImageBitmap(BitmapFactory.decodeStream(new URL("https://www.vectortemplates.com/raster/batman-logo-big.gif").openConnection().getInputStream()));
【讨论】:
试试这个:
ImageView imageview = (ImageView)findViewById(R.id.your_imageview_id);
Bitmap bmp = BitmapFactory.decodeFile(new java.net.URL(your_url).openStream());
imageview.setImageBitmap(bmp);
你也可以试试这个:Android-Universal-Image-Loader 从URL 有效地加载你的图片
【讨论】: