1

我在我的 android 应用程序中从服务器下载图像时遇到问题。
如果我尝试从https://www.morroccommethod.com/components/com_virtuemart/shop_image/category/resized/Trial_Sizes_4e4ac3b0d3491_175x175.jpg下载图片

它返回null表示图像未下载。现在,当我在服务器上替换文件名并将其复制到其他文件夹(https://www.morroccommethod.com/mm/12.jpg)只是为了测试然后图像下载成功时,我感到很惊讶。
这意味着文件名导致不下载图像。

现在我希望任何人都可以帮助和指导我。

4

1 回答 1

1

也许您没有使用正确的方法来下载图像。我不完全知道其中的问题。但我一直使用一门课,每次都对我有用。

ImageLoading.java

public class ImageLoading {

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() {  
            @Override  
            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.d(null, "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);  
            cache.put(url, new SoftReference<Bitmap>(bitmap));  
            return bitmap;  
        } catch (MalformedURLException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  

        return null;  
    }  
}  

}

并像这样在任何你想要的地方实现它-->

ImageLoading.BitmapManager.INSTANCE.loadBitmap("image_url", your_bitmapImage, width,height);
于 2012-08-23T05:34:11.103 回答