Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.graphics.Bitmap;

import java.io.BufferedOutputStream;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class Main {
    /**
     * author: liuxu
     * save  bitmap into file
     * @param bitmap the bitmap
     * @param path full path of the file
     * @return true if success
     */
    public static boolean saveBitmap(Bitmap bitmap, String path) {
        return saveBitmap(bitmap, path, 100);
    }

    /**
     * author: liuxu
     * save  bitmap into file
     * @param bitmap the bitmap
     * @param path full path of the file
     * @param quality  Hint to the compressor, 0-100. 0 meaning compress for
     *                 small size, 100 meaning compress for max quality. Some
     *                 formats, like PNG which is lossless, will ignore the
     *                 quality setting
     * @return true if success
     */
    public static boolean saveBitmap(Bitmap bitmap, String path, int quality) {
        if (bitmap == null) {
            return false;
        }
        File file = new File(path);
        File parent = file.getParentFile();
        if (parent != null && !parent.exists()) {
            if (!parent.mkdirs()) {
                //Log.e(TAG, "saveBitmap, mkdir for parent fail");
                return false;
            }
        }
        try {
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
            bitmap.compress(Bitmap.CompressFormat.JPEG, quality, bos);
            bos.flush();
            bos.close();
        } catch (IOException e) {
            //Log.d(TAG, "saveBitmap fail", e);
            return false;
        }
        return true;
    }
}