`

屏幕截图并email

阅读更多
效果图挺恶心,哈哈哈!
[img]

[/img]

布局文件就一个Button
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_text" />
</LinearLayout>


主要代码如下:
ScreenshotTools
package com.magus.screen;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Environment;
import android.os.StatFs;
import android.view.View;
import android.widget.Toast;

public class ScreenshotTools {

	/***
	 * @author Johnson
	 * 
	 * */

	public static long minSizeSDcard = 50;
	public static String filePath = Environment.getExternalStorageDirectory()
			+ "/FJBICache";
	public static String fileName = "chart.png";
	public static String detailPath = filePath + File.separator + fileName;
	public static final int SEND_EMAIL = 1;

	// public static String detailPath="/sdcard/FjbiCache/chart.png";

	/**
	 * 调用系统程序发送邮件
	 * 
	 * @author Johnson
	 * 
	 * */

	private static void sendEmail(Context context, String[] to, String subject,
			String body, String path) {

		Intent email = new Intent(android.content.Intent.ACTION_SEND);

		if (to != null) {
			email.putExtra(android.content.Intent.EXTRA_EMAIL, to);
		}
		if (subject != null) {
			email.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
		}
		if (body != null) {
			email.putExtra(android.content.Intent.EXTRA_TEXT, body);
		}
		if (path != null) {

			/*
			 * 用email.setType("image/png");或者email.setType(
			 * "application/octet-stream"); 都不会影响邮件的发送
			 * 为什么email.setType("image/png"
			 * );而不用email.setType("application/octet-stream"); ?
			 * 因为在开发中发现setType("image/png"),系统会同时给你调用彩信,邮件,等.....
			 */

			File file = new File(path);
			email.putExtra(android.content.Intent.EXTRA_STREAM,
					Uri.fromFile(file));
			email.setType("image/png");
		}
		context.startActivity(Intent.createChooser(email, "请选择发送软件"));

	}

	/**
	 * 获取指定Activity的截屏,保存到png文件
	 * 
	 * @author Johnson
	 * **/

	private static Bitmap takeScreenShot(Activity activity) {
		// View是你需要截图的View
		View view = activity.getWindow().getDecorView();
		view.setDrawingCacheEnabled(true);
		view.buildDrawingCache();
		Bitmap b1 = view.getDrawingCache();

		// 获取状态栏高度
		Rect frame = new Rect();
		activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
		int statusBarHeight = frame.top;
		System.out.println(statusBarHeight);

		// 获取屏幕长和高
		int width = activity.getWindowManager().getDefaultDisplay().getWidth();
		int height = activity.getWindowManager().getDefaultDisplay()
				.getHeight();
		// 去掉标题栏
		// Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);
		Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height
				- statusBarHeight);
		view.destroyDrawingCache();
		return b;
	}

	/**
	 * 截图保存
	 * 
	 * @author Johnson
	 * **/
	private static void savePic(Bitmap b, String filePath, String fileName) {

		File f = new File(filePath);

		if (!f.exists()) {
			f.mkdir();
		}
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(filePath + File.separator + fileName);
			if (null != fos) {
				b.compress(Bitmap.CompressFormat.PNG, 90, fos);
				fos.flush();
				fos.close();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * 截屏并发送邮件
	 * 
	 * @author Johnson
	 * **/
	public static void takeScreenShotToEmail(Context context, Activity a) {

		if (getAvailableSDcard(context)) {
			savePic(takeScreenShot(a), filePath, fileName);

			// selectDialog(context);
			sendEmail(context, null, null, null, detailPath);
		}

	}

	/***
	 * Sd判断SD卡是否可用
	 * 
	 * @author Johnson minSizeSDcard>50kb
	 * */

	public static boolean getAvailableSDcard(Context context) {

		boolean sdCardExist = Environment.getExternalStorageState().equals(
				android.os.Environment.MEDIA_MOUNTED); // 判断sd卡是否存在

		System.out.println("+++" + sdCardExist);
		if (sdCardExist) {
			File path = Environment.getExternalStorageDirectory();
			StatFs stat = new StatFs(path.getPath());
			long blockSize = stat.getBlockSize();
			long availableBlocks = stat.getAvailableBlocks();
			long sdCardSize = (availableBlocks * blockSize) / 1024;// KB值

			if (sdCardSize > minSizeSDcard) {
				System.out.println("SDcardSize:::" + minSizeSDcard + "KB");
				return true;
			} else {
				Toast.makeText(context, "SD卡空间不足", Toast.LENGTH_SHORT).show();
			}

		} else {
			Toast.makeText(context, "请在使用转发功能之前插入SD卡", Toast.LENGTH_SHORT)
					.show();

		}
		return false;
	}

}



ScreenshotActivity:
package com.magus.screen;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class ScreenshotActivity extends Activity {
    /** Called when the activity is first created. */
	
	Button bt;
	Context mContext;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        bt=(Button)findViewById(R.id.button1);
        mContext=this;
        bt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				ScreenshotTools.takeScreenShotToEmail(mContext, ScreenshotActivity.this);
			}
		});
        
        
       
    }
}


本文转载自:http://johnson008.blog.51cto.com/4000361/743048 ,谢谢作者的指教.
  • 大小: 10.4 KB
分享到:
评论

相关推荐

    Live Capture截图/截屏

    * 屏幕截图 各种输出文件方式,可以自定灵活的文件名,以及输出到Word,Excel,PPT,画图,windows图片和传真查看器,外部编辑器(可自定义),发送到Email,FTP空间,Web空间 全屏 激活窗口 窗口控件 选择区域...

    Screenpresso Pro截图工具1.5.2注册版.rar

    Screenpresso Pro是一款屏幕截图和视频截图软件,通过预先设置的截图快捷键,你可以对屏幕任意位置进行截图,截取的范围包括指定区域、全屏幕等。可以对正在播放的视频进行截图,也可以从网络摄像摄像头截图。截取...

    web截图上传和邮发到邮箱

    1.WEBScreenShot支持在IE等WEB Brower中使用屏幕截图功能,并把截图选择区以BMP/JPG/GIF格式上传到服务器或EMail到指定的邮箱中 2.用户可以任意选择截图中的区域,可以移动选择区,动态调整选择区大小,可以用笔涂鸦或用...

    优秀免费的桌面截图工具 Bug Shooting 2.17.2.849 中文版.zip

    与BUG射击,造成屏幕截图,并把他们送到各种bug跟踪和问题管理系统,电子邮件,图像编辑和即时消息应用程序是一件轻而易举的完成。 智能捕获模式 捕获 随着智能拍摄模式,这很简单,只要前所未有的捕捉画面。您可以...

    超好用右键截图工具

    是一个功能强大的屏幕图像捕捉程序, 支持鼠标和快捷键两种捕捉方式, 支持任意图形、长方形、窗口、整个桌面四种捕捉范围, 捕捉后的图片可以直接复制到剪贴板, 保存为文件, 用Email发给朋友, 通过内置的FTP软件...

    Garuda-Javascript-Syntax:语法Garuda Javascript的屏幕截图

    语法屏幕截图由Carbon( )进行专辑1个Basic.png2鹰航Component.png3 Ajax.png4 Single Data.png5 Multiple Data.png6 Input Focus Value.png7 Button Event.png8预览Image.png9从URL.png加载图像10鼠标Scroll.png11...

    IE浏览器截图插件-LightShot for

    IE浏览器截图插件LightShot for Internet ExplorerIE浏览器截图工具取代了标准的Windows PrtScr命令并允许您拍摄桌面上或整个屏幕所选区域本站提供的软件我们限于能力及系统等问题,无法保证所有软件都没有任何问题...

    redhat_linux学习笔记

    文档根据韩顺平的linux视频讲解内容为主,需要屏幕截图的email我 newbegin2020@163.com

    ClickUp: Tasks, Screenshots, Email, Time-crx插件

    1)电子邮件:创建任务并将电子邮件附加到任务2)屏幕截图:捕获,标记和编辑屏幕截图3)时间跟踪:轻松将时间附加到ClickUp任务4)创建任务5)将网站另存为任务ClickUp是永久免费的到100MB的文件存储空间。...

    经典好用的截图工具spx

    SPX Instant Screen Capture 是一个功能强大的屏幕图像捕捉程序,支持鼠 标和快捷键两种捕捉方式,支持任意图形,长方形,窗口,整个桌面四种捕捉范 围,捕捉后的图片可以直接复制到剪贴板,保存为文件,用Email发给...

    一种快速截图小软件,非常实用。

    小巧,不占据屏幕,快速方便截图,非常好用,好用!!!!!

    Webpage_Screenshot_Capture_to_Email:为第 2 页图像修改的电子邮件的网页截图捕获

    他们最初创建脚本是为了截取网页的屏幕截图,然后使用将该图像存储在您的 Google Drive 帐户中。 我修改了脚本以使用并在图片存储在 Google 云端硬盘后通过电子邮件将其发送给自己。 ###如何让它发挥作用直接取自...

    优道文档阅读器 v2.5.zip

    优道文档阅读器使用优道信息科技有限公司独创的屏幕黑洞技术进行开发,具备超强的反屏幕截图、反虚拟机及远程桌面截图功能,能对文档进行打印控制、时间控制、次数控制等,支持文档进行密码、机器码和Email验证。...

    Live Capture

    * 屏幕截图 各种输出文件方式,可以自定灵活的文件名,以及输出到Word,Excel,PPT,画图,windows图片和传真查看器,外部编辑器(可自定义),发送到Email,FTP空间,Web空间 全屏 激活窗口 窗口控件 选择区域...

    Live Capture1.2.4

    * 屏幕截图 各种输出文件方式,可以自定灵活的文件名,以及输出到Word,Excel,PPT,画图,windows图片和传真查看器,外部编辑器(可自定义),发送到Email,FTP空间,Web空间 全屏 激活窗口 窗口控件 选择区域...

    CrashReporter.NET:将使用 .NET Framework 开发的经典桌面应用程序的崩溃报告直接发送到您的邮件收件箱,其中包含完整的异常报告、堆栈跟踪和屏幕截图

    崩溃报告者.NET 将使用 .NET Framework 开发的经典桌面应用程序的崩溃报告直接发送到您的邮件收件箱,其中包含完整的异常报告、堆栈跟踪和屏幕截图。nuget 包 PM &gt; Install-Package CrashReporter.NET.Official这个...

    SPX Bundle v1.0

    集合了 Moodysoft 出品的 SPX Instant Screen Capture v5.0 屏幕截图工具、SPX Studio v2.1 截图辅助处理工具和 SPX Editor v2.0 图像编辑工具。  SPX Instant Screen Capture 是一小巧的屏幕抓图工具,可以抓取...

    Snagit 11.2简体中文注册版(附序列号).rar

    屏幕截图支持全部屏幕、区域或窗口,也可以截取滚动的窗口,可以捕获扫描仪及相机中的图像,注意:如果要捕获视频需要安装.net frameword4.0或者更高版本。可以自定义设置捕获热键,如果正运行其他截图类软件,请...

    readme-generator:这是OSU Coding训练营的作业

    链接到项目录制: 目录屏幕截图用户输入信息的屏幕截图顶级自述文件的屏幕截图README底部的屏幕截图安装您可以通过安装此项目。使用的技术:node.js用法要执行项目,请运行“ node index.js”执照贡献Keon ...

Global site tag (gtag.js) - Google Analytics