图像未从网址加载

时间:2013-01-24 04:19:01

标签: android image url imageview

在我的应用中,我必须在网址的图片视图中显示图片。 Web服务提供完整列表,其中包含与其相关的所有信息。我将所有信息存储到数据访问对象中并将其传递给详细信息页面。在我的详细信息页面上,我必须显示来自webservice的url中的图像。但是,图像没有显示出来。以下是我的代码:

图像加载器

public class ImageLoader {

public static final String TAG = "ImageLoaderSN";
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) 
    {
        //Log.v(TAG, "getBitmapFromCache called");
        if (cache.containsKey(url)) 
        {
            //Log.v(TAG, "getBitmapFromCache if loop");
            return cache.get(url).get();
        }

        return null;
    }

    public void queueJob(final String url, final ImageView imageView,
            final int width, final int height) 
    {
        Log.v(TAG, "queueJob called");
        //Create handler in UI thread. 
        final Handler handler = new Handler() 
        {
            public void handleMessage(Message msg) 
            {
                //Log.v(TAG, "queueJob handleMessage called");
                String tag = imageViews.get(imageView);
                if (tag != null && tag.equals(url)) 
                {
                    //Log.v(TAG, "queueJob tag not null");
                    if (msg.obj != null) 
                    {
                        //Log.v(TAG, "queueJob msg.obj not null");
                        imageView.setImageBitmap((Bitmap) msg.obj);
                    } else 
                    {
                        imageView.setImageBitmap(placeholder);
                        //Log.v(TAG, "queueJob 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(String url, final ImageView imageView,
            final int width, final int height) 
    {
        //Log.v(TAG, "loadBitmap called");
        if(url.contains(" "))
        {
            //Log.v(TAG, "if url contains spaces");
          url  = url.replaceAll(" ", "%20");

        }
        imageViews.put(imageView, url);
        Bitmap bitmap = getBitmapFromCache(url);

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

    public Bitmap downloadBitmap(String url, int width, int height) {
        try {

             String URL  = url.replaceAll(" ", "%20");
            Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(
                    URL).getContent());


            bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
            cache.put(URL, new SoftReference<Bitmap>(bitmap));
            return bitmap;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

     public Bitmap getResizedBitmap(Bitmap bm, int newWidth) {

            int width = bm.getWidth();

            int height = bm.getHeight();

        float aspect = (float)width / height;

        float scaleWidth = newWidth;

        float scaleHeight = scaleWidth / aspect;        // yeah!

        // create a matrix for the manipulation

        Matrix matrix = new Matrix();

        // resize the bit map

        matrix.postScale(scaleWidth / width, scaleHeight / height);

        // recreate the new Bitmap

        Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);

            bm.recycle();

            return resizedBitmap;
        }

}

}

检查后,我发现图片网址不为空。但是,当缓存中没有位图时,ImageLoader类中queueJob()中的处理程序没有运行。我独立测试了相同的代码,并且它有效。无法找出实际问题是什么。

0 个答案:

没有答案