下载的图像无法显示

下载的图像无法显示

本文介绍了下载的图像无法显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我previously曾在列表视图中显示它从SD卡获取图像,曾使用:

I previously worked on fetching image from sd card displaying it in a list view, that worked using:

imgView.setImageURI(Uri.parse(ImagePath));

现在,我想从网址显示的图像,通过下面的代码,但图像没有在列表视图中显示,以下是使用的行:

Now, I am trying to display image from URL, with the following lines but the image is not displayed in the list view, the following are the lines used:

imgView.setImageBitmap(getBitmapFromURL(ImagePath));

在哪里,是getBitmapFromURL:

Where, getBitmapFromURL is:

public static Bitmap getBitmapFromURL(String src) {
        try {
            URL url = new URL(src);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            return myBitmap;
            }
        catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

有没有异常显示,图像只是没有得到显示。

There is no exception displayed, the image just not get displayed.

这是亟待解决的极品......

Need of an urgent solution....

谢谢,

推荐答案

这是一个同步加载。(如果个人有这么多的图像加载我不会用这个原因,应用程序是有点laggy)..

This is a synchronous loading.(Personally I would not use this cause if there are so many Image to be loaded, the apps is a bit laggy)..

URL url = new URL(//your URL);
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
imageView.setImageBitmap(bmp);//your imageview

如果我是你,我会研究异步或懒惰的适配器。

If I were you I would study Async or the lazy adapter..

修改
我忘了在那里我得到这些code(以及感谢您精彩的code作者)

EDITI forgot where I got these code (well thank you for a wonderful code author)

  public Bitmap getBitmap(String bitmapUrl) {
      try {
        URL url = new URL(bitmapUrl);
        return BitmapFactory.decodeStream(url.openConnection().getInputStream());
      }
      catch(Exception ex) {return null;}
    }

    public enum BitmapManager {
    INSTANCE;

    private final Map<String, SoftReference<Bitmap>> cache;
    private final ExecutorService pool;
    private Map<ImageView, String> imageViews = Collections
            .synchronizedMap(new WeakHashMap<ImageView, String>());
    private Bitmap placeholder;

    BitmapManager() {
        cache = new HashMap<String, SoftReference<Bitmap>>();
        pool = Executors.newFixedThreadPool(5);
    }

    public void setPlaceholder(Bitmap bmp) {
        placeholder = bmp;
    }

    public Bitmap getBitmapFromCache(String url) {
        if (cache.containsKey(url)) {
            return cache.get(url).get();
        }

        return null;
    }

    public void queueJob(final String url, final ImageView imageView,
            final int width, final int height) {
        /* Create handler in UI thread. */
        final Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                String tag = imageViews.get(imageView);
                if (tag != null && tag.equals(url)) {
                    if (msg.obj != null) {
                        imageView.setImageBitmap((Bitmap) msg.obj);
                    } else {
                        imageView.setImageBitmap(placeholder);
                        Log.d(null, "fail " + url);
                    }
                }
            }
        };

        pool.submit(new Runnable() {

            public void run() {
                final Bitmap bmp = downloadBitmap(url, width, height);
                Message message = Message.obtain();
                message.obj = bmp;
                Log.d(null, "Item downloaded: " + url);

                handler.sendMessage(message);
            }
        });
    }

    public void loadBitmap(final String url, final ImageView imageView,
            final int width, final int height) {
        imageViews.put(imageView, url);
        Bitmap bitmap = getBitmapFromCache(url);


        // check in UI thread, so no concurrency issues
        if (bitmap != null) {
            Log.i("inh","Item loaded from cache: " + url);
            imageView.setImageBitmap(bitmap);
        } else {
            imageView.setImageBitmap(placeholder);
            queueJob(url, imageView, width, height);
        }
    }

    private Bitmap downloadBitmap(String url, int width, int height) {
        try {
            Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(
                    url).getContent());

            bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
            Log.i("nandi2 ako", ""+bitmap);
            cache.put(url, new SoftReference<Bitmap>(bitmap));
            return bitmap;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

}

现在叫它

String fbAvatarUrl = "//Your URL";

    BitmapManager.INSTANCE.loadBitmap(fbAvatarUrl, //Your ImageView, 60,60);
            //60 60 is my desired height and width

这篇关于下载的图像无法显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 16:43