0

I use the following code do get an image from a regular image url:

try
{
  url = new URL("http://example.com/test.jpg");
  final Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
  final ImageView imageView=(ImageView)findViewById(R.id.imageView2);
  MainActivity.this.runOnUiThread(new Runnable()
  {
    @Override
    public void run()
    {
      imageView.setImageBitmap(bmp);
    }
  });
}
catch (Exception e)
{
  e.printStackTrace(); 
}

However, I want to download images that are displayed by html or php pages.

  url = new URL("http://example.com/showimage.php");

If I use this, I get the following message:

SkImageDecoder::Factory returned null

How should I modify my code?

Nestor
  • 8,194
  • 7
  • 77
  • 156
  • `http://example.com/showimage.php`: is it an image or a webpage? – Benjamin Toueg Oct 29 '13 at 22:14
  • It can be two types: 1. it contains a redirect to a valid image 2. it consists of an _img_ tag. Will the load work in any cases? – Nestor Oct 29 '13 at 22:16
  • you will have to parse the html from that file to get the direct image links for the source, as the current code will not just take all the images from the page and download automatically. – kabuto178 Oct 29 '13 at 22:17
  • If it contains an `img` tag, it won't work. I think you would need to extract the `href` attribute of your img tag and then call directly this `url`. – Benjamin Toueg Oct 29 '13 at 22:18
  • What about the redirect? It eventually shows a valid image. – Nestor Oct 29 '13 at 22:19

2 Answers2

1

I recommend you to look at this library: http://jsoup.org/

SerhatCan
  • 590
  • 1
  • 7
  • 26
  • It could be useful, too. I would prefer an out-of-the-box image downloader that handles everything :) – Nestor Oct 30 '13 at 14:15
0

This seems to be the solution when the page is redirected: http://blog.kosev.net/2011/01/follow-302-redirects-with.html

Nestor
  • 8,194
  • 7
  • 77
  • 156