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.FileOutputStream;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import android.app.Instrumentation;

import android.graphics.Bitmap;
import android.util.Log;
import android.view.View;

public class Main {
    public final static String TAG = "andtools";

    public static void takeScreenshot(String fileName, String directory, int quality, Instrumentation inst) {
        Method takeScreenshot;
        Method getUiAutomation;
        Object mUiAutomationVaule;
        Bitmap bitmap = null;
        if (android.os.Build.VERSION.SDK_INT < 18) {
            Log.e(TAG, "Build.VERSION is :" + android.os.Build.VERSION.SDK_INT + ", it should >= API 18");
            return;
        }
        try {
            getUiAutomation = Instrumentation.class.getDeclaredMethod("getUiAutomation");
            mUiAutomationVaule = getUiAutomation.invoke(inst, new Object[] {});
            takeScreenshot = mUiAutomationVaule.getClass().getDeclaredMethod("takeScreenshot", new Class[] {});
            if (mUiAutomationVaule != null)
                bitmap = (Bitmap) takeScreenshot.invoke(mUiAutomationVaule, new Object[] {});
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(directory + File.separator + fileName);
            if (fileName.endsWith(".png")) {
                bitmap.compress(Bitmap.CompressFormat.PNG, quality, fos);
            } else if (fileName.endsWith(".jpg")) {
                bitmap.compress(Bitmap.CompressFormat.JPEG, quality, fos);
            }
            fos.flush();
            fos.close();
        } catch (Exception e) {
            Log.e(TAG,
                    "Can't save the screenshot! Requires write permission (android.permission.WRITE_EXTERNAL_STORAGE) in AndroidManifest.xml of the application under test.");
            e.printStackTrace();
        }
    }

    public static void takeScreenshot(String fileName, String directory, View view, int quality) {
        Bitmap bitmap = null;
        FileOutputStream fos = null;
        view.buildDrawingCache(false);
        bitmap = view.getDrawingCache();
        try {
            fos = new FileOutputStream(directory + File.separator + fileName);
            if (fileName.endsWith(".png")) {
                bitmap.compress(Bitmap.CompressFormat.PNG, quality, fos);
            } else if (fileName.endsWith(".jpg")) {
                bitmap.compress(Bitmap.CompressFormat.JPEG, quality, fos);
            }
            fos.flush();
            fos.close();
        } catch (Exception e) {
            Log.e(TAG,
                    "Can't save the screenshot! Requires write permission (android.permission.WRITE_EXTERNAL_STORAGE) in AndroidManifest.xml of the application under test.");
            e.printStackTrace();
        }
    }
}