Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;

import android.graphics.Rect;
import android.util.Log;
import android.view.View;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class Main {
    public static String getCrop(Activity activity, String name) {
        Bitmap bmp = getCrop(activity);
        File file = new File(activity.getFilesDir(), name);
        FileOutputStream fileOutput = null;
        try {
            fileOutput = new FileOutputStream(file);
        } catch (FileNotFoundException e) {
            Log.e("golfpad", e.getMessage());
        }
        bmp.compress(CompressFormat.JPEG, 50, fileOutput);
        return file.getAbsolutePath();
    }

    public static Bitmap getCrop(Activity activity) {
        View view = activity.getWindow().getDecorView();
        view.buildDrawingCache();

        Rect rect = new Rect();
        view.getWindowVisibleDisplayFrame(rect);
        int stateBarHeight = rect.top;

        view.setDrawingCacheEnabled(true);

        Bitmap bmpCache = view.getDrawingCache();
        Bitmap bmp = Bitmap.createBitmap(bmpCache, 0, stateBarHeight, bmpCache.getWidth(),
                bmpCache.getHeight() - stateBarHeight);

        view.destroyDrawingCache();

        return bmp;
    }
}