Beispiel #1
1
  public Bitmap getBitmap(String fileId, String size, boolean fromUrl) {
    File file = fileCache.getFile(fileId + size);

    // from SD cache
    Bitmap bitmap = decodeFile(file, size);

    if (bitmap != null) {
      return bitmap;
    }

    // from web
    try {
      bitmap = null;

      if (fromUrl) {
        InputStream is =
            ConnectionHandler.httpGetRequest(fileId, UsersManagement.getLoginUser().getId());
        OutputStream os = new FileOutputStream(file);
        Utils.copyStream(is, os);
        os.close();
        is.close();
      } else {
        CouchDB.downloadFile(fileId, file);
      }

      bitmap = decodeFile(file, size);

      return bitmap;
      // return ConnectionHandler.getBitmapObject(url);
    } catch (Throwable ex) {
      ex.printStackTrace();
      if (ex instanceof OutOfMemoryError) memoryCache.clear();
      return null;
    }
  }
Beispiel #2
0
  public Bitmap getBitmap(String url) {
    File f = fileCache.getFile(url);

    // from SD cache
    Bitmap bitmap = decodeFile(f);

    if (bitmap != null) {
      Logger.error("ImageLoader", "fileCache.returnBitmap : " + url);
      return bitmap;
    }

    // from web
    try {

      bitmap = null;
      InputStream is =
          ConnectionHandler.httpGetRequest(url, UsersManagement.getLoginUser().getId());
      OutputStream os = new FileOutputStream(f);
      Utils.copyStream(is, os);
      os.close();
      is.close();
      // conn.disconnect();
      bitmap = decodeFile(f);

      return bitmap;
      // return ConnectionHandler.getBitmapObject(url);
    } catch (Throwable ex) {
      ex.printStackTrace();
      if (ex instanceof OutOfMemoryError) memoryCache.clear();
      return null;
    }
  }
  /*
   * This function is strictly for use by internal APIs. Not that we have
   * anything external but there is some trickery here! The getBitmap function
   * cannot be invoked from the UI thread. Having to deal with complexity of
   * when & how to call this API is too much for those who just want to have
   * the bitmap. This is a utility function and is public because it is to be
   * shared by other components in the internal implementation.
   */
  public Bitmap getBitmap(
      String serverUrl, boolean cacheBitmap, int bestWidth, int bestHeight, String source) {
    File f = mFileCache.getFile(serverUrl);
    // from SD cache
    Bitmap b = decodeFile(f, bestHeight, bestWidth, source);
    if (b != null) {
      Logging.i(TAG, "Image Available in SD card: ", false, classLevelLogEnabled);
      if (cacheBitmap) mAisleImagesCache.putBitmap(serverUrl, b);
      return b;
    }

    // from web
    try {
      if (serverUrl == null || serverUrl.length() < 1) {

        return null;
      }
      Logging.i(
          TAG,
          "Image DownLoad Time starts At: " + System.currentTimeMillis(),
          false,
          classLevelLogEnabled);
      Bitmap bitmap = null;
      URL imageUrl = new URL(serverUrl);
      HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();
      conn.setConnectTimeout(30000);
      conn.setReadTimeout(30000);
      conn.setInstanceFollowRedirects(true);
      InputStream is = conn.getInputStream();
      int hashCode = serverUrl.hashCode();
      String filename = String.valueOf(hashCode);
      OutputStream os = new FileOutputStream(f);
      Utils.CopyStream(is, os);
      os.close();
      Logging.i(
          TAG,
          "Image DownLoad Time Ends At: " + System.currentTimeMillis(),
          false,
          classLevelLogEnabled);
      Logging.i(
          TAG,
          "Bitmap Decode Time Starts At: " + System.currentTimeMillis(),
          false,
          classLevelLogEnabled);
      bitmap = decodeFile(f, bestHeight, bestWidth, source);
      Logging.i(
          TAG,
          "Bitmap Decode Time Ends At: " + System.currentTimeMillis(),
          false,
          classLevelLogEnabled);
      if (cacheBitmap) mAisleImagesCache.putBitmap(serverUrl, bitmap);
      return bitmap;
    } catch (Throwable ex) {
      ex.printStackTrace();
      if (ex instanceof OutOfMemoryError) {
        // mAisleImagesCache.evictAll();
      }
      return null;
    }
  }
  private Bitmap getBitmap(String url) {
    File f = fileCache.getFile(url);

    // from SD cache
    Bitmap b = decodeFile(f);
    if (b != null) return b;

    // from web
    try {
      Bitmap bitmap = null;
      URL imageUrl = new URL(url);
      HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();
      conn.setConnectTimeout(30000);
      conn.setReadTimeout(30000);
      conn.setInstanceFollowRedirects(true);
      InputStream is = conn.getInputStream();
      OutputStream os = new FileOutputStream(f);
      Utils.CopyStream(is, os);
      os.close();
      conn.disconnect();
      bitmap = decodeFile(f);
      return bitmap;
    } catch (Throwable ex) {
      ex.printStackTrace();
      if (ex instanceof OutOfMemoryError) memoryCache.clear();
      return null;
    }
  }
Beispiel #5
0
  /**
   * download url theo height va width
   *
   * @author: truonglt2
   * @param url
   * @param requestWidth
   * @param requestHeight
   * @return
   * @return: Bitmap
   * @throws:
   */
  public Bitmap getBitmap(String url, int requestWidth, int requestHeight) {
    File f = fileCache.getFile(url);

    // from SD cache
    Bitmap b = decodeFile(f, requestWidth, requestHeight);
    if (b != null) return b;

    // from web
    try {
      Bitmap bitmap = null;
      URL imageUrl = new URL(url);
      HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();
      conn.setConnectTimeout(30000);
      conn.setReadTimeout(30000);
      conn.setInstanceFollowRedirects(true);
      InputStream is = conn.getInputStream();
      OutputStream os = new FileOutputStream(f);
      Utils.CopyStream(is, os);
      os.close();
      bitmap = decodeFile(f, requestWidth, requestHeight);
      return bitmap;
    } catch (Exception ex) {
      ex.printStackTrace();
      return null;
    }
  }
  public Bitmap getBitmap(String url) throws IOException {
    File f = fileCache.getFile(url);

    // from SD cache
    Bitmap b = decodeFile(f);
    if (b != null) return b;
    OutputStream os = null;

    // from web
    try {
      Bitmap bitmap = null;
      URL imageUrl = new URL(url);
      HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();
      conn.setConnectTimeout(60000);
      conn.setReadTimeout(60000);
      conn.setInstanceFollowRedirects(true);
      InputStream is = conn.getInputStream();
      os = new FileOutputStream(f);
      Utils.CopyStream(is, os);
      bitmap = decodeFile(f);
      return bitmap;
    } finally {
      try {
        if (os != null) os.close();
      } catch (IOException e) {
        throw new RuntimeException(e);
      }
    }
  }
Beispiel #7
0
  private Bitmap getBitmap(String url, int requiredSize) {
    File f = fileCache.getFile(url);

    Bitmap b = decodeFile(f, requiredSize);
    if (b != null) return b;

    Utils.logDebug(TAG, "load image from " + url);
    Bitmap bitmap = null;
    InputStream is = null;
    OutputStream os = null;
    try {
      HttpTransport httpTransport = new HttpTransport();
      is = httpTransport.executeGetRequest(url);
      os = new FileOutputStream(f);
      Utils.copyStream(is, os);
      bitmap = decodeFile(f, requiredSize);
    } catch (Throwable ex) {
      ex.printStackTrace();
      if (ex instanceof OutOfMemoryError) memoryCache.clear();
    } finally {
      if (os != null) {
        try {
          os.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
    return bitmap;
  }
Beispiel #8
0
  private Bitmap getBitmap(String url) {
    File f = fileCache.getFile(url);

    // from SD cache
    Bitmap b = Util.decodeFile(f);
    if (b != null) return b;

    // from web
    return mNetwork.httpGetBitmap(url, f);
  }
 /**
  * ** display a image from the network
  *
  * @param url image url
  * @param view imageView
  * @return null
  */
 public void DisplayImage(String url, ImageView view) {
   imageViews.put(view, url);
   Bitmap bitmap = null; // memoryCache.get(url);
   File f = fileCache.getFile(url);
   if (f != null) bitmap = BitmapUtil.decodeFile(f, 480);
   if (bitmap != null) {
     view.setImageBitmap(BitmapUtil.handleReflection(bitmap));
   } else {
     // Log.e("no cache in memory" , "downloading!");
     queuePhoto(url, view);
     Bitmap bitmap1 = BitmapFactory.decodeResource(context.getResources(), R.drawable.placeholder);
     view.setImageBitmap(BitmapUtil.handleReflection(bitmap1));
   }
 }
  /**
   * 执行网络请求加载图片
   *
   * @param url
   * @param requiredSize
   * @return
   */
  private Bitmap getBitmap(String url, int requiredSize, PhotoToLoad photoToLoad) {
    File f = fileCache.getFile(url);
    // 先从文件缓存中查找是否有
    Bitmap b = decodeFile(f, requiredSize);
    if (b != null) return b;

    // 最后从指定的url中下载图片
    try {
      Bitmap bitmap = null;
      URL imageUrl = new URL(url);
      HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();
      conn.setConnectTimeout(30000);
      conn.setReadTimeout(30000);
      conn.setInstanceFollowRedirects(true);
      InputStream is = conn.getInputStream();
      OutputStream os = new FileOutputStream(f);
      //            CopyStream(is, os, conn.getContentLength(), photoToLoad);

      photoToLoad.totalSize = conn.getContentLength();
      int buffer_size = 1024;
      byte[] bytes = new byte[buffer_size];
      for (; ; ) {
        int count = is.read(bytes, 0, buffer_size);

        if (count == -1) {
          break;
        }
        os.write(bytes, 0, count);

        if (null != photoToLoad.onImageLoaderListener) { // 如果设置了图片加载监听,则回调
          Message msg = rHandler.obtainMessage();
          photoToLoad.currentSize += count;
          msg.arg1 = IMAGE_LOADER_PROCESS;
          msg.obj = photoToLoad;
          rHandler.sendMessage(msg);
        }
      }

      is.close();
      os.close();
      bitmap = decodeFile(f, requiredSize);
      return bitmap;
    } catch (Exception ex) {
      Logger.w(TAG, ex);
      return null;
    }
  }
  private Bitmap getBitmap(String url) {
    // File f=fileCache.getFile(url);

    File f = fileCache.getFile(url);

    // from SD cache
    // CHECK : if trying to decode file which not exist in cache return null
    Bitmap b = decodeFile(f);
    if (b != null) return b;

    // Download image file from web
    try {
      System.out.println("Downloading image::" + url);
      Bitmap bitmap = null;
      URL imageUrl = new URL(url);
      HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();
      conn.setConnectTimeout(30000);
      conn.setReadTimeout(30000);
      conn.setInstanceFollowRedirects(true);
      InputStream is = conn.getInputStream();

      // Constructs a new FileOutputStream that writes to file
      // if file not exist then it will create file
      OutputStream os = new FileOutputStream(f);

      // See Utils class CopyStream method
      // It will each pixel from input stream and
      // write pixels to output stream (file)
      Utils.CopyStream(is, os);

      os.close();
      conn.disconnect();

      // Now file created and going to resize file with defined height
      // Decodes image and scales it to reduce memory consumption
      bitmap = decodeFile(f);

      return bitmap;

    } catch (Throwable ex) {
      ex.printStackTrace();
      if (ex instanceof OutOfMemoryError) memoryCache.clear();
      return null;
    }
  }
  // To get Image URI from url
  public Uri getImageUri(String url) {

    File f = fileCache.getFile(url);
    if (f == null) {
      return null;
    }
    File imageFile = new File(f.getParent() + "/fununlimitedJoke.png");
    /* Bitmap bmp= memoryCache.get(url); */

    Uri uri = null;
    try {
      Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(f), null, null);
      FileOutputStream out = new FileOutputStream(imageFile);
      bitmap.compress(Bitmap.CompressFormat.PNG, 85, out);
      out.flush();
      out.close();
      uri = Uri.fromFile(imageFile);
    } catch (Exception e) {
      // TODO Auto-generated catch block
      Log.e("Exception While Sharing Joke", "Get File URI CAlled");
      return null;
    }
    return uri;
  }
  public String getInternet(Context context, String site) {
    // The data that is retrieved
    String result = null;
    FileCache cache = new FileCache(context);
    File cacheFile = cache.getFile(site);
    String fileName = cacheFile.getAbsolutePath();

    if (this.checkInternetConnection(context)) {
      if (!cache.verifyCache(fileName)) {
        try {
          // This assumes that you have a URL from which the response will come
          URL url = new URL(site);

          // Open a connection to the URL and obtain a buffered input stream
          URLConnection connection = url.openConnection();
          InputStream inputStream = connection.getInputStream();
          BufferedInputStream bufferedInput = new BufferedInputStream(inputStream, 8);

          // Read the response into a byte array
          ByteArrayBuffer byteArray = new ByteArrayBuffer(50);
          int current = 0;
          while ((current = bufferedInput.read()) != -1) {
            byteArray.append((byte) current);
          }

          // Construct a String object from the byte array containing the response
          result = new String(byteArray.toByteArray());
          try {
            if (cache.saveFile(result)) {
              Log.i("INTERNET", "Cache salvo!");
            } else {
              Log.i("INTERNET", "Erro no salvar o cache!");
            }
          } catch (Exception e) {
            result = "seminternet";
            Log.e("INTERNET", e.toString());
          }
        } catch (Exception e) {
          result = "seminternet";
          Log.e("INTERNET", e.toString());
        }
      } else {
        try {
          result = cache.readFile(fileName);
        } catch (Exception e) {
          result = "seminternet";
          Log.e("INTERNET", e.toString());
        }
      }
    } else {
      try {
        result = cache.readFile(fileName);
        if (result.length() == 0) {
          result = "seminternet";
        }
      } catch (Exception e) {
        result = "seminternet";
        Log.e("INTERNET", e.toString());
      }
    }
    Log.i("INTERNET", result);
    return (result);
  }