App Inventor Extensions


Take Screenshot Extension

See the App Inventor Extensions document about how to use an App Inventor Extension.

For questions about this extension or bug reports please start a new thread in the App Inventor community. Thank you.

For feature requests please contact me by email. To be a sponsor of a new method already is possible starting from only 10 USD! With your contribution you will help the complete App Inventor community. Thank you.

Aug 11th, 2016: Version 1a: avoid DX execution failed error: build each extension separately

Sep 8th, 2016: Version 1b: provide error message in case something goes wrong

Sep 15th, 2021: Version 2: SDK30 adjustment to store the screenshot in the ASD - Application Specific Directory

Description

Take Screenshot Extension to take a screenshot.
Required permission: none

Note: You also might be interested in the Screenshot Service Extension, which is able to take screenshots while your app is not running...

Properties


Return fileName of screenshot to be taken. Default filename is screenshot.jpg.


Set fileName of screenshot to be taken.

Methods


Take a screenshot.

Events


Return image.

Example Use

For questions about App Inventor,
please ask in the App Inventor community.Thank you.

Questions and Answers

Q1: I am unable to get the screenshot of a web page. When I open it with web viewer, it shows perfectly but when I use the screenshot extension to share the screenshot of that, the chart area is removed automatically. Please let me know about any other ways or extensions which i could you for this.
A: Try the Component to Image Screenshot Extension by Jerin Jacob, which also can take screenshots of a webviewer page.

Q2: how to make user cannot take screenshot when open our apps?
A: Add the Disable Screen Capture Extension by Ken to every screen you want to prevent screenshots. Thank you Ken!

Java source code (Version 1)

package com.puravidaapps;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.View;
import android.app.Activity;
import android.os.Environment;

import com.google.appinventor.components.annotations.DesignerComponent;
import com.google.appinventor.components.annotations.DesignerProperty;
import com.google.appinventor.components.annotations.PropertyCategory;
import com.google.appinventor.components.annotations.SimpleEvent;
import com.google.appinventor.components.annotations.SimpleFunction;
import com.google.appinventor.components.annotations.SimpleObject;
import com.google.appinventor.components.annotations.SimpleProperty;
import com.google.appinventor.components.annotations.UsesPermissions;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.common.PropertyTypeConstants;
import com.google.appinventor.components.runtime.*;

import java.io.File;
import java.io.FileOutputStream;


@DesignerComponent(version = TaifunScreenshot.VERSION,
    description = "Extension to take a screenshot.",
    category = ComponentCategory.EXTENSION,
    nonVisible = true,
    iconName = "https://puravidaapps.com/images/taifun16.png")
@SimpleObject(external = true)
@UsesPermissions(permissionNames = "android.permission.WRITE_EXTERNAL_STORAGE")

public class TaifunScreenshot extends AndroidNonvisibleComponent implements Component {

  public static final int VERSION = 1;
  private ComponentContainer container;
  private Context context;
  private final Activity activity;
  private String fileName;
  private static final String LOG_TAG = "TaifunScreenshot";


  public TaifunScreenshot(ComponentContainer container) {
    super(container.$form());
    this.container = container;
    context = (Context) container.$context();
    activity = (Activity) container.$context();
    fileName = "screenshot.jpg";
    Log.d(LOG_TAG, "TaifunScreenshot Created" );
  }



  /**
   * Return fileName
   */
  @SimpleProperty(category = PropertyCategory.BEHAVIOR,
      description = "filename of the screenshot to be taken")
  public String FileName() {
    return fileName;
  }


  /**
   * Set fileName
   */
  @DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_STRING,
      defaultValue = "screenshot.jpg")
  @SimpleProperty
  public void FileName(String fileName) {
    this.fileName = fileName;
  }


  /**
   * Take a screenshot
   */
  @SimpleFunction(description = "Take a screenshot.")
  public void TakeScreenshot() {
    // "http://stackoverflow.com/a/5651242/1545993
    try {
      // image naming and path  to include sd card  appending name you choose for file
      String mPath = Environment.getExternalStorageDirectory().toString() + File.separator + fileName;

      // create bitmap screen capture
      View v1 = activity.getWindow().getDecorView().getRootView();
      v1.setDrawingCacheEnabled(true);
      Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
      v1.setDrawingCacheEnabled(false);

      File imageFile = new java.io.File(mPath);

      FileOutputStream outputStream = new FileOutputStream(imageFile);
      int quality = 100;
      bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
      outputStream.flush();
      outputStream.close();

      Log.d(LOG_TAG, "screenshot taken: " + imageFile.toString());
      AfterScreenshot(imageFile.toString());
    } catch (Throwable e) {
      // Several error may come out with file handling or OOM
      Log.e(LOG_TAG, e.getMessage(), e);
      e.printStackTrace();
    }
  }

  /**
   * The AfterScreenshot event
   */
  @SimpleEvent(description = "Return image.")
  public void AfterScreenshot(String image){
    EventDispatcher.dispatchEvent(this, "AfterScreenshot", image);
  }

}

Terms and Conditions

Download


Developing and maintaining snippets, tutorials and extensions for App Inventor takes a lot of time.
I hope it saved some of your time. If yes, then you might consider to donate a small amount!

Donation amount:

or donate some mBTC to Bitcoin Address:
1Jd8kXLHu2Vkuhi15TWHiQm4uE9AGPYxi8
Bitcoin

Thank you! Taifun
 

Download TaifunScreenshot extension (aix file)
Back to top of page ...

Creative Commons License
This work by Pura Vida Apps is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License
with attribution (name=Pura Vida Apps and link to the source site) required.


Home | Snippets | Tutorials | Extensions | Links | Search | Privacy Policy | Contact