Saturday, August 20, 2011

UtilMethod For Android - I

I have collected some Utility method from my code.

/** Return a specific file contents as a String value. */
public static String getFileString(File file) throws Exception {
    String result = null;
    InputStream is = null;
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    try {
        is = new FileInputStream(file);
        int bytesRead;
        byte[] buffer = new byte[1024];
        while ((bytesRead = is.read(buffer)) != -1) {
            os.write(buffer, 0, bytesRead);
        }
        os.flush();
        result = new String(os.toByteArray());
    } catch(Exception e) {
        throw new Exception("Problem while reading file", e);
    } finally {
        try {
            os.close();
            is.close();
        } catch(Exception e) {
        }
    }
    return result;
}

/** Return a specific url contents as a String value. */

public static String getUrlString(String remoteUrl) throws Exception {
    String result = null;
    InputStream is = null;
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    try {
        URL url = new URL(remoteUrl);
        URLConnection connection = url.openConnection();
        connection.setConnectTimeout(5000);
        connection.setReadTimeout(5000);
        connection.connect();
        is = connection.getInputStream();
        int bytesRead;
        byte[] buffer = new byte[1024];
        while ((bytesRead = is.read(buffer)) != -1) {
            os.write(buffer, 0, bytesRead);
        }

        os.flush();
        result = new String(os.toByteArray());
    } catch(Exception e) {
        throw new Exception("Problem while reading targeted url", e);
    } finally {
        try {
            os.close();
            is.close();
        } catch(Exception e) {
        }
    }
    return result;
}

/** Create Thumbnail Image
* Param1 : Image,
* Param2 : Required Size */


public static Drawable createIconThumbnail(Drawable icon, int size) {
    int sourceWidth = icon.getIntrinsicWidth(),
    sourceHeight = icon.getIntrinsicHeight();
   
    int destWidth = size, destHeight = size;
   
    // only resize if actually needed
    if(sourceWidth != destWidth || sourceHeight != destHeight) {
        float ratio = (float) sourceWidth / sourceHeight;
        if(sourceWidth > sourceHeight) {
            destHeight = (int) (destWidth / ratio);
        } else if (sourceHeight > sourceWidth) {
            destWidth = (int) (destHeight * ratio);
        }

        final Bitmap thumb = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
        final Canvas canvas = new Canvas(thumb);

        icon.setBounds((size - destWidth) / 2, (size - destHeight) / 2, destWidth, destHeight);
        icon.draw(canvas);
        icon = new BitmapDrawable(thumb);
    }
    return icon;
}

/** Return a current time a String value. */
 private String getCurrentTimeString() {
        Calendar calendar = Calendar.getInstance();
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);
        return String.format("%02d:%02d:%02d ", hour, minute, second);
    }