Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.graphics.Bitmap;

import android.graphics.Matrix;

public class Main {
    public static boolean saveMyBitmap(File f, Bitmap mBitmap) throws IOException {
        boolean saveComplete = true;
        try {
            f.createNewFile();
            FileOutputStream fOut = null;
            fOut = new FileOutputStream(f);
            int width = mBitmap.getWidth();
            int height = mBitmap.getHeight();
            int finalWidth = 800;
            int finalHeight = (int) (finalWidth * 1.0 * (height * 1.0 / width * 1.0));
            double x = width * finalHeight;
            double y = height * finalWidth;

            if (x > y) {
                finalHeight = (int) (y / (double) width);
            } else if (x < y) {
                finalWidth = (int) (x / (double) height);
            }

            if (finalWidth > width && finalHeight > height) {
                finalWidth = width;
                finalHeight = height;
            }
            Matrix matrix = new Matrix();
            matrix.reset();
            float scaleWidth = ((float) finalWidth) / (float) width;
            float scaleHeight = ((float) finalHeight) / (float) height;
            matrix.postScale(scaleWidth, scaleHeight);
            mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, (int) width, (int) height, matrix, true);
            mBitmap.compress(Bitmap.CompressFormat.JPEG, 80, fOut);
            fOut.flush();
            fOut.close();
            mBitmap.recycle();
            System.gc();
        } catch (FileNotFoundException e) {
            saveComplete = false;
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
            saveComplete = false;
        }
        return saveComplete;
    }
}