问题描述
我想知道如何从给定的URL下载图像,并在ImageView 中显示它.在manifest.xml文件中需要提及permissions?
推荐答案
需要将此permission放入Inte.net
<uses-permission android:name="android.permission.INTERNET" />
您可以尝试此代码.
String imageurl = "YOUR URL"; InputStream in = null; try { Log.i("URL", imageurl); URL url = new URL(imageurl); URLConnection urlConn = url.openConnection(); HttpURLConnection httpConn = (HttpURLConnection) urlConn; httpConn.connect(); in = httpConn.getInputStream(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } Bitmap bmpimg = BitmapFactory.decodeStream(in); ImageView iv = "YOUR IMAGE VIEW"; iv.setImageBitmap(bmpimg);
其他推荐答案
使用背景线程获取图像,并在使用Hanndler中将图像设置为ImageView之后.
new Thread(){ public void run() { try { Bitmap bitmap = BitmapFactory.decodeStream(new URL("http://imageurl").openStream()); Message msg = new Message(); msg.obj = bitmap; imageHandler.sendMessage(msg); } catch (Exception e) { e.printStackTrace(); } } }.start();
处理程序代码,我们在imgaeview中设置下载的图像.
Handler imageHandler = new Handler(){ public void handleMessage(Message msg) { if(msg.obj!=null && msg.obj instanceof Bitmap){ imageview.setBackgroundDrawable(new BitmapDrawable((Bitmap)msg.obj)); } }; };
和您需要互联网权限.
<uses-permission android:name="android.permission.INTERNET" />
其他推荐答案
您需要在Android清单文件中设置INTERNET的权限,并使用java.net.URL和java.net.URLConnection类到请求 URL.
问题描述
I'd like to know how I can download an Image from a given URL and display it inside an ImageView. And is there any permissions required to mention in the manifest.xml file?
推荐答案
You need to put this permission to access the Internet
<uses-permission android:name="android.permission.INTERNET" />
you can try this code.
String imageurl = "YOUR URL"; InputStream in = null; try { Log.i("URL", imageurl); URL url = new URL(imageurl); URLConnection urlConn = url.openConnection(); HttpURLConnection httpConn = (HttpURLConnection) urlConn; httpConn.connect(); in = httpConn.getInputStream(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } Bitmap bmpimg = BitmapFactory.decodeStream(in); ImageView iv = "YOUR IMAGE VIEW"; iv.setImageBitmap(bmpimg);
其他推荐答案
Use background thread to get image and after getting image set it in imageview using hanndler.
new Thread(){ public void run() { try { Bitmap bitmap = BitmapFactory.decodeStream(new URL("http://imageurl").openStream()); Message msg = new Message(); msg.obj = bitmap; imageHandler.sendMessage(msg); } catch (Exception e) { e.printStackTrace(); } } }.start();
Handler code where we set downloaded image in imgaeview.
Handler imageHandler = new Handler(){ public void handleMessage(Message msg) { if(msg.obj!=null && msg.obj instanceof Bitmap){ imageview.setBackgroundDrawable(new BitmapDrawable((Bitmap)msg.obj)); } }; };
And ofcourse you need internet permission.
<uses-permission android:name="android.permission.INTERNET" />
其他推荐答案
You need to set usage permission of INTERNET in android manifest file and use java.net.URL and java.net.URLConnection classes to request the URL.