通过Android手机截取屏幕截图

时间:2014-02-18 10:08:07

标签: android

我如何通过android屏幕捕获。我的意思是我想编写一个程序,它可以制作屏幕截图。非常感谢所有链接,谢谢大家

2 个答案:

答案 0 :(得分:3)

这不可能像你想的那样容易。出于安全原因,没有官方API。

以下是您的选择:

  • 使用screencap命令行工具:需要root权限,未在所有手机上安装
  • 获取帧缓冲区:需要root
  • 使用android-screenshot-library:需要adb连接才能启动

答案 1 :(得分:1)

我使用此代码截取我的APP(您无法拍摄其他应用)的屏幕截图

@SuppressLint("SimpleDateFormat")
protected final static boolean shoot
(final View v, final String appName)
{
    // Get the bitmap from the view
    v.setDrawingCacheEnabled(true);

    boolean isOK = false;

    final Bitmap bmp = v.getDrawingCache();

    final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd@HHmmss");
    final Calendar cal = Calendar.getInstance();

    // Set file properties
    fileJPG = appName + "_" + sdf.format(cal.getTime());

    /*
    Create a path where we will place our picture in the user's public
    pictures directory. Note that you should be careful about what you
    place here, since the user often manages these files.
    For pictures and other media owned by the application, consider
    Context.getExternalMediaDir().
    */
    final File path =
        Environment.getExternalStoragePublicDirectory
        (
            Environment.DIRECTORY_DCIM + "/Your_App_Name/"
        );

    // Make sure the Pictures directory exists.
    if(!path.exists())
    {
        path.mkdirs();
    }

    final File file = new File(path, fileJPG + ".jpg");

    try
    {
        final FileOutputStream fos = new FileOutputStream(file);
        final BufferedOutputStream bos =
            new BufferedOutputStream(fos, 8192);

        bmp.compress(CompressFormat.JPEG, 85, bos);

        bos.flush();
        bos.close();

        fileJPG = file.getPath();
        isOK = true;
    }
    catch (final IOException e)
    {
        e.printStackTrace();
    }
    return isOK;
}