如何从网址ImageView的设定图像

编程入门 行业动态 更新时间:2024-10-17 23:31:38
本文介绍了如何从网址ImageView的设定图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想使用URL,比如我有这个网址中的ImageView设置图片

I wanna set Image in ImageView using Url for example I have this url

www.google.iq/imgres?hl=en&biw=1366&bih=667&tbm=isch&tbnid=HjzjsaANDXVR9M:&imgrefurl=www.vectortemplates/raster-batman.php&docid=FxbVmggVf--0dM&imgurl=www.vectortemplates/raster/batman-logo-big.gif&w=2072&h=1225&ei=Zeo_UoSWIMaR0AXl_YHIBg&zoom=1

但没有选项来设置网址

推荐答案

编辑:

通过使用AsychTask

With using AsychTask

木箱类像

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); } }

和调用这个像新ImageLoadTask(URL,ImageView的).execute();

And call this like new ImageLoadTask(url, imageView).execute();

直接法:

使用这种方法,并通过你的网址字符串,它retuns你的位图,然后将其设置为ImageView的..

Use this method and pass your url as string it retuns you a bitmap then set it to 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如..

And then this to ImageView like..

imageView.setImageBitmap(getBitmapFromURL(url));

和不要忘记在maifest此权限。

And dont forget about this permission in maifest.

<uses-permission android:name="android.permission.INTERNET" />

注意:

Try to call this method from another thread or AsynchTask because we are performing networking operations.

更多推荐

如何从网址ImageView的设定图像

本文发布于:2023-11-07 10:48:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1566249.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   网址   ImageView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!