Search code examples
androiddrawablepicasso

cannot get to load images from a Link in android studio


I have this code, where it gets images from drawables folder. I need to get images form URLs. I think I can do it using Picasso and I tried and I could not nail it.

I have this code in my MainActivity:

public Drawable LoadImageFromWebOperations(String url) {
    try {
        InputStream is = (InputStream) new URL(url).getContent();
        Drawable d = Drawable.createFromStream(is, "image_name");
        return d;
    } catch (Exception e) {
        return null;
    }
}

private Drawable d1 = LoadImageFromWebOperations("http://uupload.ir/files/aud7_brickone.jpg");

private List<App> getApps() {
    List<App> apps = new ArrayList<>();
    apps.add(new App("Google+", d1, 4.6f));
    apps.add(new App("Google+", d1, 4.6f));
    apps.add(new App("Google+", d1, 4.6f));
    apps.add(new App("Google+", d1, 4.6f));
    apps.add(new App("Google+", d1, 4.6f));
    apps.add(new App("Google+", d1, 4.6f));
    apps.add(new App("Google+", d1, 4.6f));
    return apps;
}

and this is my adapter:

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    App app = mApps.get(position);
    holder.imageView.setImageResource(app.getDrawable());
    holder.nameTextView.setText(app.getName());
    holder.ratingTextView.setText(String.valueOf(app.getRating()));
}

and here APP.JAVA

public class App {
    private Drawable mDrawable;
    private String mName;
    private float mRating;

    public App (String name, Drawable drawable, float rating){
        mName = name;
        mDrawable = drawable;
        mRating = rating;
    }

    public float getRating (){return mRating;}
    public Drawable getDrawable (){return mDrawable;}
    public String getName (){return mName;}
}

I need to the images from link like: http://uupload.ir/files/aud7_brickone.jpg

I cannot make it happen!


Solution

  • There are different ways to download an image from an URL. Check the funcion below:

    public static Drawable LoadImageFromWebOperations(String url) {
        try {
            InputStream is = (InputStream) new URL(url).getContent();
            Drawable d = Drawable.createFromStream(is, "any_image_name");
            return d;
        } catch (Exception e) {
            return null;
        }
    }
    

    Then simply show the returned drawable in your ImageView.

    Don't forget to add the internet permissions in your manifest:

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

    You could also use this other method, combined with an AsyncTask or a background Thread:

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

    Then you call it wherever you need it with:

    new DownloadImageTask((ImageView) findViewById(R.id.imageView))
            .execute("http://uupload.ir/files/aud7_brickone.jpg");
    

    Hope it helps :)