Android常用方法

/* * 保存一张图片到指定的路径
*
* @author jksfood
*
* @param bitmap
* bitmap 是JPG
*
* @param filePath
* 图片要保存的位置
*
* @return
*/
public static boolean saveBitmapToSd(Bitmap bitmap, String filePath) {
FileOutputStream outputStream = null;
try {
File file = new File(filePath);
if (file.exists() || file.isDirectory()) {
file.delete();
}
file.createNewFile();
outputStream = new FileOutputStream(file);
bitmap.compress(CompressFormat.JPEG, 0, outputStream);
} catch (IOException e) {
// if(Log.debug){
//
// e.printStackTrace();
//
// }
return false;

    } finally {
        if (outputStream != null) {
            try {
                    outputStream.flush();
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return true;
}
    /**
     * 从SD卡读Bitmap
     * 
     * @author jksfood
     * @param filePath
     * @param reqWidth
     * @param reqHeight
     * @return
     */

    public static Bitmap getBitmapFromSdCard(String filePath, int reqWidth,
            int reqHeight) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, options);
        options.inSampleSize = calculateInSampleSize(options, reqWidth,
                reqHeight);

        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(filePath, options);
    }

    /**
     * 计算样本大小
     * 
     * @author jksfood
     * @param options
     * @param reqWidth
     * @param reqHeight
     * @return
     */
    private static int calculateInSampleSize(BitmapFactory.Options options,
            int reqWidth, int reqHeight) {
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;
        if (height > reqHeight || width > reqWidth) {
            final int heightRatio = Math.round((float) height
                    / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);
            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
        }
        return inSampleSize;
    }
/**
 * 从内存卡读取图片(防止OOM)
 * 
 * @author jksfood
 * @param fileName
 * @param height
 * @return
 */

public static Bitmap getBitmpFromSDCard(String fileName, int height) {
    Bitmap b = null;
    BitmapFactory.Options o2 = null;
    try {
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;

        FileInputStream fis;

        fis = new FileInputStream(fileName);

        BitmapFactory.decodeStream(fis, null, o);
        fis.close();

        int scale = o.outHeight / height;

        o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        fis = new FileInputStream(fileName);
        b = BitmapFactory.decodeStream(fis, null, o2);
        fis.close();
    } catch (IOException e) {
    } catch (OutOfMemoryError e) {
        System.gc();
    }
    return b;
}

    /**
     * Drable转为Bitmap
     * @author jksfood
     * @param d
     * @return
     */

    public static Bitmap getDrawable2Bitmap(Drawable d) {
        BitmapDrawable bd = (BitmapDrawable) d;
        Bitmap bm = bd.getBitmap();
        return bm;
    }

    /**
     * 缩放图片
     * 
     * @author jksfood
     * @Time 2014年8月27日 上午11:32:21
     * @param paramBitmap
     *            原图
     * @param newWidth
     *            新的宽度
     * @param newHeight
     *            新的高度
     * @return
     */
    public static Bitmap getScaleBitmap2Bitmap(Bitmap paramBitmap,
            int newWidth, int newHeight) {
        int i = paramBitmap.getWidth();
        int j = paramBitmap.getHeight();
        float f1 = ((float) newWidth) / i;
        float f2 = ((float) newHeight) / j;
        if (f1 <= 0.0F) {
            f1 = 1.0F;
        }
        if (f2 <= 0.0F) {
            f2 = 1.0F;
        }
        Matrix localMatrix = new Matrix();
        localMatrix.postScale(f1, f2);
        return Bitmap.createBitmap(paramBitmap, 0, 0, i, j, localMatrix, true);
    }
/**
 * 基于位移的Integer转化成byte[]
 * 
 * @author jksfood
 * @param number
 * @return
 */
public static byte[] intToByte(int number) {
    byte[] abyte = new byte[4];
    // "&" 与(AND),对两个整型操作数中对应位执行布尔代数,两个位都为1时输出1,否则0。
    abyte[0] = (byte) (0xff & number);
    // ">>"右移位,若为正数则高位补0,若为负数则高位补1
    abyte[1] = (byte) ((0xff00 & number) >> 8);
    abyte[2] = (byte) ((0xff0000 & number) >> 16);
    abyte[3] = (byte) ((0xff000000 & number) >> 24);
    return abyte;
}

    /**
     * 基于位移的 byte[]转化成Integer
     * 
     * @author jksfood
     * @param bytes
     * @return
     */
    public static int bytesToInt(byte[] bytes) {
        int number = bytes[0] & 0xFF;
        // "|="按位或赋值。
        number |= ((bytes[1] << 8) & 0xFF00);
        number |= ((bytes[2] << 16) & 0xFF0000);
        number |= ((bytes[3] << 24) & 0xFF000000);
        return number;
    }
/**
 * 合并两个byte[]为一个byte[]
 * 
 * @author YOLANDA
 * @param bytes1
 * @param bytes2
 * @return
 */
public static byte[] combineTowBytes(byte[] bytes1, byte[] bytes2) {
    byte[] bytes3 = new byte[bytes1.length + bytes2.length];
    System.arraycopy(bytes1, 0, bytes3, 0, bytes1.length);
    System.arraycopy(bytes2, 0, bytes3, bytes1.length, bytes2.length);
    return bytes3;
}

    /**
     * 产生一个0-end的随机数
     * 
     * @author jksfood
     * @param n
     * @return
     */
    public static int getRandomInt(int end) {
        Random random = new Random();
        return random.nextInt(end);
    }
      ```
    /**
     * 生成一个给定范围的随机数
     * 
     * @author jksfood
     * @param min
     * @param max
     * @return
     */
    public static int getRandomInt(int min, int max) {
        Random random = new Random();
        return random.nextInt(max) % (max - min + 1) + min;
    }
/**
 * 是否是数字
 * 
 * @author jksfood
 * @param value
 * @return
 */
public static boolean isInteger(String value) {
    Pattern pattern = Pattern.compile("[0-9]*");
    return pattern.matcher(value).matches();
}
    /**
     * 重命名文件
     * 
     * @author jksfood
     * @param resFilePath
     * @param newFilePath
     * @return
     */
    public static boolean renameFile(String resFilePath, String newFilePath) {
        File resFile = new File(resFilePath);
        File newFile = new File(newFilePath);
        return resFile.renameTo(newFile);
    }
/**
 * android控制闪光灯
 */  

public class LightControl {

private boolean isOpen;

private Camera mCamera;

public LightControl() {
    if (mCamera == null) {
        mCamera = Camera.open();
    }
}

/**
 * 传入已经打开的相机实例
 * 
 * @author jksfood
 * @param camera
 */
public LightControl(Camera camera) {
    this.mCamera = camera;
}

/**
 * 是否是打开的
 * 
 * @author jksfood
 * @return
 */
public boolean isOpen() {
    return isOpen;
}

/**
 * 打开
 * 
 * @author jksfood
 */
public void open() {
    try {
        Camera.Parameters mParameters = mCamera.getParameters();
        mParameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
        mCamera.setParameters(mParameters);
        isOpen = true;
    } catch (Exception ex) {
        isOpen = false;
    }

}

/**
 * 关闭
 * 
 * @author jksfood
 */
public void close() {
    try {
        Camera.Parameters mParameters = mCamera.getParameters();
        mParameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
        mCamera.setParameters(mParameters);
        isOpen = true;
    } catch (Exception ex) {
        isOpen = false;
    }

}
}

    /** 关闭输入法
     * 
     * @author YOLANDA
     * @param context
     */
    public static void closeInputMethod(Activity context) {
        InputMethodManager inputMethodManager = (InputMethodManager) context
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        View focusView = context.getCurrentFocus();
        if (inputMethodManager != null && focusView != null) {
            inputMethodManager.hideSoftInputFromWindow(
                    focusView.getWindowToken(),
                    InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容