1

While calling my url, which downloads an image from server automatically. I need to display this image on an Android ImageView. I usually using Piccaso library to load image from url but that not helping me here, is there any way to load image from auto download url to an Android ImageView?

Auto download url example is in here(wallpaperswide.com/download/bike_chase-wallpaper-2560x1600.jpg)

Shanto George
  • 994
  • 13
  • 26

1 Answers1

0

Well if dont want to use library like Piccaso try something like this:

public class AsyncTaskLoadImage  extends AsyncTask<String, String, Bitmap> {
    private final static String TAG = "AsyncLoadImage"; 
    private ImageView imageView;
    public AsyncTaskLoadImage(ImageView imageView) {
    this.imageView = imageView;
    }
    @Override
    protected Bitmap doInBackground(String... params) {
        Bitmap bitmap = null;
        try {
            URL url = new URL(params[0]);
                bitmap = BitmapFactory.decodeStream((InputStream)url.getContent());
    } catch (IOException e) {
        Log.e(TAG, e.getMessage());
    }
        return bitmap;
    }
    @Override
    protected void onPostExecute(Bitmap bitmap) {
       StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
       StrictMode.setThreadPolicy(policy);              
       imageView.setImageBitmap(bitmap);
    }
}

And you call in your activity like:

String url = "YOUR_LINK_HERE";
new AsyncTaskLoadImage(imageView).execute(url);

OR

With less code try something like this answer:

URL url = new URL("YOUR_LINK_HERE");
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
imageView.setImageBitmap(bmp);
Paraskevas Ntsounos
  • 1,755
  • 2
  • 18
  • 34