Code example for InputStream

Methods: createFromStreamclose

0
     * @return 
     */ 
    public static Drawable getDrawableFromUrl(String imageUrl, int readTimeOutMillis,
                                              Map<String, String> requestProperties) {
        InputStream stream = getInputStreamFromUrl(imageUrl, readTimeOutMillis, requestProperties);
        Drawable d = Drawable.createFromStream(stream, "src");
        closeInputStream(stream);
        return d;
    } 
 
    /** 
     * get Bitmap by imageUrl 
     *  
     * @param imageUrl 
     * @param readTimeOut 
     * @return 
     * @see ImageUtils#getBitmapFromUrl(String, int, boolean) 
     */ 
    public static Bitmap getBitmapFromUrl(String imageUrl, int readTimeOut) {
        return getBitmapFromUrl(imageUrl, readTimeOut, null);
    } 
 
    /** 
     * get Bitmap by imageUrl 
     *  
     * @param imageUrl 
     * @param requestProperties http request properties 
     * @return 
     */ 
    public static Bitmap getBitmapFromUrl(String imageUrl, int readTimeOut, Map<String, String> requestProperties) {
        InputStream stream = getInputStreamFromUrl(imageUrl, readTimeOut, requestProperties);
        Bitmap b = BitmapFactory.decodeStream(stream);
        closeInputStream(stream);
        return b;
    } 
 
    /** 
     * scale image 
     *  
     * @param org 
     * @param newWidth 
     * @param newHeight 
     * @return 
     */ 
    public static Bitmap scaleImageTo(Bitmap org, int newWidth, int newHeight) {
        return scaleImage(org, (float)newWidth / org.getWidth(), (float)newHeight / org.getHeight());
    } 
 
    /** 
     * scale image 
     *  
     * @param org 
     * @param scaleWidth sacle of width 
     * @param scaleHeight scale of height 
     * @return 
     */ 
    public static Bitmap scaleImage(Bitmap org, float scaleWidth, float scaleHeight) {
        if (org == null) {
            return null; 
        } 
 
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        return Bitmap.createBitmap(org, 0, 0, org.getWidth(), org.getHeight(), matrix, true);
    } 
 
    /** 
     * close inputStream 
     *  
     * @param s 
     */ 
    private static void closeInputStream(InputStream s) {
        if (s == null) {
            return; 
        } 
 
        try { 
            s.close();
        } catch (IOException e) {
            throw new RuntimeException("IOException occurred. ", e);
        } 
    } 
}