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 java.io.ByteArrayOutputStream;
import java.io.File;

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

import android.graphics.Bitmap;

public class Main {

    public static boolean saveJPEGBitmap(Bitmap aBmp, String aPath) {
        if (aBmp == null || aPath == null) {
            return false;
        }

        FileOutputStream fos = null;
        ByteArrayOutputStream baos = null;
        boolean result = false;
        try {
            File file = new File(aPath);
            if (!file.exists()) {
                file.createNewFile();
            }
            fos = new FileOutputStream(file);
            baos = new ByteArrayOutputStream();
            aBmp.compress(Bitmap.CompressFormat.JPEG, 100, baos); //SUPPRESS CHECKSTYLE
            fos.write(baos.toByteArray());
            baos.flush();
            fos.flush();

            result = true;
        } catch (Error e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
            result = false;
        } finally {
            try {
                if (baos != null) {
                    baos.close();
                }
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }
}