解决方案 »

  1.   

    UniversalImageLoader 这个还可以。
    不过你可以自己实现一个,不用那么麻烦。
      

  2.   

    /**
     * 根据url获得Bitmap,必须在子线程中使用该方法
     * @param url
     * @return
     * @throws Exception
     */
    public Bitmap getBitmapByUrl(String url) throws Exception {
    Bitmap bitmap = ImageService.get().getBitmap(url);
    if (null != bitmap) {return bitmap;}
    if ((viewUtil.isWifi()) || (viewUtil.isNet() && !SettingService.get().getSetting(SettingService.WIFI_IMG))) {
    bitmap = fileUtil.getBitmap(url);
    if (null != bitmap) {
    String folder = fileUtil.getContextRoot() + Domain.CACHE_IMAGE;fileUtil.createFolder(folder);
    String imagePath = folder + UpdaterService.get().getImgName(url);
    String ext = imagePath.substring(imagePath.lastIndexOf("."));
    CompressFormat format = (ImageUtil.JPG.equals(ext) || ImageUtil.JPEG.equals(ext) ? Bitmap.CompressFormat.JPEG : Bitmap.CompressFormat.PNG);
    fileUtil.saveBitmap(bitmap, imagePath, format);
    ImageService.get().add(url, imagePath);
    }
    }
    return bitmap;
    }/**
     * 根据url获得图片来源
     * @param url 完整的可访问的url
     * @return
     * @throws Exception
     */
    public Bitmap getBitmap(String url) {
    try {
    if (!ViewUtil.get().isNet()) {
    throw new RuntimeException(AbstractActivity.NET_ERR_MSG);
    }
    HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
    conn.connect();InputStream input = conn.getInputStream();
    Bitmap bitmap = BitmapFactory.decodeStream(input);
    input.close();
    conn.disconnect();
    return ImageUtil.get().reduce(bitmap, Container.winSize[0], Container.winSize[1], true);
    } catch (Exception e) {
    Log.e("AbstractActivity", "根据url获得图片来源", e);
    return null;
    }
    }
    /**
     * 把Bitmap保存为文件
     * @param bitmap 图片源
     * @param filePath 文件路径,要完整的
     * @param format 保存的格式
     * @throws Exception
     */
    public void saveBitmap(Bitmap bitmap, String filePath, CompressFormat format) throws Exception {
    OutputStream output = new FileOutputStream(filePath);
    bitmap.compress(format, 100, output);
    output.flush();
    output.close();
    }