我想使用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的设定图像
发布评论