Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.FileInputStream;

import java.io.IOException;

import android.content.Context;

import android.graphics.BitmapFactory;

public class Main {
    private static Context sCurrentContext;

    /**
     * <pre>
     * Get width and height of bitmap from byte array.
     * </pre>
     * @param bmData bitmap binary data
     * @return integer array with 2 elements: width and height
     */
    public static int[] getBitmapSizes(byte[] bmData) {
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        bmOptions.inJustDecodeBounds = true;
        BitmapFactory.decodeByteArray(bmData, 0, bmData.length, bmOptions);
        return new int[] { bmOptions.outWidth, bmOptions.outHeight };
    }

    /**
     * <pre>
     * Get width and height of bitmap from resource.
     * </pre>
     * @param resId resource id of bitmap
     * @return integer array with 2 elements: width and height
     */
    public static int[] getBitmapSizes(int resId) {
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        bmOptions.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(getCurrentContext().getResources(), resId, bmOptions);
        return new int[] { bmOptions.outWidth, bmOptions.outHeight };
    }

    /**
     * <pre>
     * Get width and height of bitmap from file.
     * </pre>
     * @param path path to file
     * @return integer array with 2 elements: width and height
     */
    public static int[] getBitmapSizes(String path) throws IOException {
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        bmOptions.inJustDecodeBounds = true;
        FileInputStream is = new FileInputStream(path);
        BitmapFactory.decodeStream(is, null, bmOptions);
        is.close();
        return new int[] { bmOptions.outWidth, bmOptions.outHeight };
    }

    /**
     * <pre>
     * Get current context of the app. This method resolves the inconvenience of Android which requires context for most of its API.
     * If no activity is resumed, this method returns application context. Otherwise, this method returns last resumed activity.
     * </pre>
     */
    public static Context getCurrentContext() {
        return sCurrentContext;
    }
}