Example usage for android.view View buildDrawingCache

List of usage examples for android.view View buildDrawingCache

Introduction

In this page you can find the example usage for android.view View buildDrawingCache.

Prototype

@Deprecated
public void buildDrawingCache(boolean autoScale) 

Source Link

Document

Forces the drawing cache to be built if the drawing cache is invalid.

If you call #buildDrawingCache() manually without calling #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true) , you should cleanup the cache by calling #destroyDrawingCache() afterwards.

Note about auto scaling in compatibility mode: When auto scaling is not enabled, this method will create a bitmap of the same size as this view.

Usage

From source file:Main.java

/**
 * Calls {@link View#buildDrawingCache(boolean)}.  If you call this
 * manually without calling {@link View#setDrawingCacheEnabled(boolean)
 * setDrawingCacheEnabled(true)}, you should clean up by calling
 * {@link View#destroyDrawingCache()} afterward.
 *
 * @param view what to build a drawing cache of
 *//* w ww. j a v a  2  s  .  c om*/
public static void buildDrawingCache(View view, boolean autoScale) {
    view.buildDrawingCache(autoScale);
}

From source file:Main.java

public static Bitmap loadBitmapFromView(View v) {
    v.setDrawingCacheEnabled(true);/*  w w  w. j av a  2s.c om*/
    v.buildDrawingCache(true);
    // creates immutable clone
    Bitmap b = Bitmap.createBitmap(v.getDrawingCache());

    v.setDrawingCacheEnabled(false); // clear drawing cache
    return b;
}

From source file:Main.java

public static Bitmap takeScreenShot(View view) {
    view.setDrawingCacheEnabled(true);//from   w w w  . ja  va2s.  c  om
    view.buildDrawingCache(true);
    Bitmap res = Bitmap.createBitmap(view.getDrawingCache());
    view.setDrawingCacheEnabled(false);
    return res;
}

From source file:Main.java

/**
 * Take the screen shot of the device/* w ww .ja  v a  2s .  co m*/
 * 
 * @param view
 */
public static void screenShotMethod(View view) {
    Bitmap bitmap;
    if (view != null) {
        View v1 = view;
        v1.setDrawingCacheEnabled(true);
        v1.buildDrawingCache(true);
        bitmap = Bitmap.createBitmap(v1.getDrawingCache());
        v1.setDrawingCacheEnabled(false);

        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
        File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator
                + "CySmart" + File.separator + "file.jpg");
        try {
            FileOutputStream fo = new FileOutputStream(f);
            fo.write(bytes.toByteArray());
            fo.flush();
            fo.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

From source file:Main.java

public static void takeScreenshot(String fileName, String directory, View view, int quality) {
    Bitmap bitmap = null;/* ww  w.  j a  v a 2  s  .c o  m*/
    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();
    }
}

From source file:Main.java

public static Bitmap getScaledScreenshot(final Activity activity, int scaleWidth, int scaleHeight,
        boolean relativeScaleIfTrue) {
    final View someView = activity.findViewById(android.R.id.content);
    final View rootView = someView.getRootView();
    final boolean originalCacheState = rootView.isDrawingCacheEnabled();
    rootView.setDrawingCacheEnabled(true);
    rootView.buildDrawingCache(true);

    // We could get a null or zero px bitmap if the rootView hasn't been measured
    // appropriately, or we grab it before layout.
    // This is ok, and we should handle it gracefully.
    final Bitmap original = rootView.getDrawingCache();
    Bitmap scaled = null;/*from   w w  w .j  a  va2 s.com*/
    if (null != original && original.getWidth() > 0 && original.getHeight() > 0) {
        if (relativeScaleIfTrue) {
            scaleWidth = original.getWidth() / scaleWidth;
            scaleHeight = original.getHeight() / scaleHeight;
        }
        if (scaleWidth > 0 && scaleHeight > 0) {
            try {
                scaled = Bitmap.createScaledBitmap(original, scaleWidth, scaleHeight, false);
            } catch (OutOfMemoryError error) {
                Log.i(LOGTAG, "Not enough memory to produce scaled image, returning a null screenshot");
            }
        }
    }
    if (!originalCacheState) {
        rootView.setDrawingCacheEnabled(false);
    }
    return scaled;
}

From source file:Main.java

public static Bitmap getViewBitmap(View view) {
    view.setDrawingCacheEnabled(true);/*from w w w.  j  a  v  a2  s .  c o m*/
    view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
    view.buildDrawingCache(true);
    Bitmap bm = Bitmap.createBitmap(view.getDrawingCache());
    view.setDrawingCacheEnabled(false);
    return bm;
}

From source file:Main.java

public static Bitmap convertViewToBitmap(View v) {
    v.setDrawingCacheEnabled(true);/*from  ww w  .j a  v a2  s .co  m*/
    v.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());

    v.buildDrawingCache(true);
    Bitmap b = Bitmap.createBitmap(v.getDrawingCache());
    v.setDrawingCacheEnabled(false); // clear drawing cache
    return b;
}

From source file:Main.java

public static Bitmap getViewBitmap(View v) {
    v.setDrawingCacheEnabled(true);/*from  w  ww .  j  av  a  2  s . com*/

    // this is the important code :)
    // Without it the view will have a dimension of 0,0 and the bitmap will
    // be null
    v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());

    v.buildDrawingCache(true);
    Bitmap bmp = Bitmap.createBitmap(v.getDrawingCache());
    v.setDrawingCacheEnabled(false); // clear drawing cache

    return bmp;
}

From source file:com.groksolutions.grok.mobile.GrokActivity.java

/**
 * Capture the screen and return the URI of the image
 *///from ww  w  . ja  v  a2s.c  om
private Uri takeScreenCapture(boolean isRetryOk) {
    String fileName = "GROK_" + new SimpleDateFormat("yyyyMMddhhmm'.jpg'", Locale.US).format(new Date());

    File screenShot = new File(getCacheDir(), fileName);

    // create bitmap screen capture
    View v1 = getWindow().getDecorView().getRootView();
    v1.setDrawingCacheEnabled(true);
    v1.invalidate();
    v1.buildDrawingCache(true);
    Bitmap bitmap = null;
    FileOutputStream fOut = null;

    try {
        bitmap = Bitmap.createBitmap(v1.getDrawingCache(true));
        v1.setDrawingCacheEnabled(false);
        fOut = new FileOutputStream(screenShot);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 75, fOut);

    } catch (FileNotFoundException e) {
        Log.e(TAG, "Screen shot file not found", e);
    } catch (OutOfMemoryError e) {
        Log.e(TAG, "Out of Memory Error creating screenshot", e);
        // retry one time on out of memory
        if (isRetryOk) {
            return takeScreenCapture(false);
        }
        return writeTextFileToSend("screenshot.txt", "Out of Memory: Failed to generate screenshot");
    } finally {
        // recycle the bitmap on the heap to free up space and help prevent
        // out of memory errors
        if (bitmap != null) {
            bitmap.recycle();
            bitmap = null;
        }
        System.gc();
        try {
            if (fOut != null) {
                fOut.flush();
                fOut.close();
            }
        } catch (IOException e) {
            Log.e(TAG, "Error saving the screenshot file", e);
        }
    }

    return Uri.parse("content://" + getApplication().getPackageName() + "/" + fileName);
}