Load image from url

前端 未结 16 1987
独厮守ぢ
独厮守ぢ 2020-11-22 05:12

I have an image URL. I want to display an image from this URL in an ImageView but I am unable to do that.

How can this be achieved?

相关标签:
16条回答
  • 2020-11-22 05:25

    To me, Fresco is the best among the other libraries.

    Just setup Fresco and then simply set the imageURI like this:

    draweeView.setImageURI(uri);
    

    Check out this answer explaining some of Fresco benefits.

    0 讨论(0)
  • 2020-11-22 05:26

    The accepted answer above is great if you are loading the image based on a button click, however if you are doing it in a new activity it freezes up the UI for a second or two. Looking around I found that a simple asynctask eliminated this problem.

    To use an asynctask to add this class at the end of your activity:

    private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
      ImageView bmImage;
    
      public DownloadImageTask(ImageView bmImage) {
          this.bmImage = bmImage;
      }
    
      protected Bitmap doInBackground(String... urls) {
          String urldisplay = urls[0];
          Bitmap mIcon11 = null;
          try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
          } catch (Exception e) {
              Log.e("Error", e.getMessage());
              e.printStackTrace();
          }
          return mIcon11;
      }
    
      protected void onPostExecute(Bitmap result) {
          bmImage.setImageBitmap(result);
      }
    }
    

    And call from your onCreate() method using:

    new DownloadImageTask((ImageView) findViewById(R.id.imageView1))
            .execute(MY_URL_STRING);
    

    Dont forget to add below permission in your manifest file

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

    Works great for me. :)

    0 讨论(0)
  • 2020-11-22 05:26

    Try this:

    InputStream input = contentResolver.openInputStream(httpuri);
    Bitmap b = BitmapFactory.decodeStream(input, null, options);
    
    0 讨论(0)
  • 2020-11-22 05:27
    URL url = new URL("http://image10.bizrate-images.com/resize?sq=60&uid=2216744464");
    Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
    imageView.setImageBitmap(bmp);
    
    0 讨论(0)
  • 2020-11-22 05:29

    Best Method I have tried instead of using any libraries

    public Bitmap getbmpfromURL(String surl){
        try {
            URL url = new URL(surl);
            HttpURLConnection urlcon = (HttpURLConnection) url.openConnection();
            urlcon.setDoInput(true);
            urlcon.connect();
            InputStream in = urlcon.getInputStream();
            Bitmap mIcon = BitmapFactory.decodeStream(in);
            return  mIcon;
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
            return null;
        }
    }
    
    0 讨论(0)
  • 2020-11-22 05:34
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.widget.ImageView;
    import android.widget.Toast;
    
    public class imageDownload {
    
        Bitmap bmImg;
        void downloadfile(String fileurl,ImageView img)
        {
            URL myfileurl =null;
            try
            {
                myfileurl= new URL(fileurl);
    
            }
            catch (MalformedURLException e)
            {
    
                e.printStackTrace();
            }
    
            try
            {
                HttpURLConnection conn= (HttpURLConnection)myfileurl.openConnection();
                conn.setDoInput(true);
                conn.connect();
                int length = conn.getContentLength();
                int[] bitmapData =new int[length];
                byte[] bitmapData2 =new byte[length];
                InputStream is = conn.getInputStream();
                BitmapFactory.Options options = new BitmapFactory.Options();
    
                bmImg = BitmapFactory.decodeStream(is,null,options);
    
                img.setImageBitmap(bmImg);
    
                //dialog.dismiss();
                } 
            catch(IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
    //          Toast.makeText(PhotoRating.this, "Connection Problem. Try Again.", Toast.LENGTH_SHORT).show();
            }
    
    
        }
    
    
    }
    

    in your activity take imageview & set resource imageDownload(url,yourImageview);

    0 讨论(0)
提交回复
热议问题