How to download a image from URL in App

后端 未结 5 530
长情又很酷
长情又很酷 2021-01-03 14:40

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 men

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-03 15:25

    You need to put this permission to access the 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);  
    

提交回复
热议问题