Android根据byte数组,生成图片,图片名字以当前时间作为名字

/**
* 根据byte数组生成图片,图片名字以当前时间作为名字
*/

public static String saveJPGFile(Context mContext, byte[] data, String key) {
    if (data == null)
        return null;


    File mediaStorageDir = mContext
            .getExternalFilesDir(Constant.cacheImage);


    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            return null;
        }
    }


    BufferedOutputStream bos = null;
    FileOutputStream fos = null;
    try {
        String jpgFileName = System.currentTimeMillis() + ""
                + new Random().nextInt(1000000) + "_" + key + ".jpg";
        fos = new FileOutputStream(mediaStorageDir + "/" + jpgFileName);
        bos = new BufferedOutputStream(fos);
        bos.write(data);
        return mediaStorageDir.getAbsolutePath() + "/" + jpgFileName;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (bos != null) {
            try {
                bos.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }
    return null;
}
适用java,只要去掉一下Context就好,换成自己的FileUtil即可。


你可能感兴趣的:(Android开发)