2

I want to display image to ImageView from "http://i.imgur.com/4GLKF4Q.jpg" but The image in image URL didn't display to imageview. Can I do this?

AsyncTask

class DownloadImageTask extends AsyncTask<String, Void, byte[]> {
    ImageView bmImage;

    public DownloadImageTask(ImageView bmImage) {
        this.bmImage = bmImage;
    }

    protected byte[] doInBackground(String... urls) {
        try {
            URL url = new URL(urls[0]);
            InputStream is = (InputStream) url.getContent();
            byte[] buffer = new byte[8192];
            int bytesRead;
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            while ((bytesRead = is.read(buffer)) != -1) {
                output.write(buffer, 0, bytesRead);
            }
            return output.toByteArray();
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    protected void onPostExecute(byte[] result) {
        ByteArrayInputStream imageStream = new ByteArrayInputStream(result);
        Bitmap bm = BitmapFactory.decodeStream(imageStream);
        bmImage.setImageBitmap(bm);
    }
}

in activity

new DownloadImageTask((ImageView) newView.findViewById(R.id.thumbnail))
            .execute("http://i.imgur.com/4GLKF4Q.jpg");

The image in image URL didn't display to imageview. in Logcat :

09-23 10:26:49.410 10591-10591/com.*** D/skia: --- SkImageDecoder::Factory returned null
2
  • Better way to do this is to user AQuery code.google.com/p/android-query or Picasso github.com/square/picasso Commented Sep 23, 2015 at 9:04
  • This question has been asked many times but most of the answers are out dated. Modern way is to use NetworkImageView please see my answer below. Commented Sep 23, 2015 at 9:29

3 Answers 3

1

Update from comment : changing image server to another one solve the issue. I guess it's a i.imgur.com issue.


You shouldn't use your own image loader task, it will leak somewhere. Two good library exist : - Fresco (from Facebook) - Picasso (from Square up)

There is also UniversalImageLoader (UIL).

Sign up to request clarification or add additional context in comments.

7 Comments

I used picasso and result is same. Logcat log is D/skia: --- SkImageDecoder::Factory returned null . When I used different image URL, it worked. i.imgur.com - server isn't work.
Hmm I guess somethink is wonrg with this image maybe, try Fresco and see if t's happen too. If yes, it could be a software issue on your device
i used fresco and SimpleDraweeView . Logcat log is --- Caused by: java.lang.NullPointerException: SimpleDraweeView was not initialized!
as @TahaKörkem If think something is wrong either from the image, from the server or from your software decoder on your device.
anymore I using different image server, it's working. thanks for comments
|
1
  1. I would suggest you to checkout Image Management Library by Facebook that is Fresco that is pretty awesome and mature as compared to other Image Loading Library.

  2. Fresco handles all the things caching of images with 3 Tier architecture ( BITMAP_MEMORY_CACHE, ENCODED_MEMORY_CACHE and DISK_CACHE). It also reduces OOM(Out Of Memory) issues. When image in a view goes out of screen it automatically recycles the bitmap, hence releasing the memory.

Comments

0

Now there is an official way to load an ImageView from a URL and that is NOT to use an ImageView.

NetworkImageView—builds on ImageLoader and effectively replaces ImageView for situations where your image is being fetched over the network via URL. NetworkImageView also manages canceling pending requests if the view is detached from the hierarchy.

Images are automatically loaded in a background thread and the view updated on the UI thread. It even supports caching.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.