Android image didn't display to ImageView from image URL

雨燕双飞 提交于 2019-12-12 21:47:42

问题


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

回答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).




回答2:


  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.




回答3:


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.



来源:https://stackoverflow.com/questions/32733569/android-image-didnt-display-to-imageview-from-image-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!