Android开发实例:实现屏幕截图及邮件发送功能

移动开发 Android
我们大家在用电脑的时候,都用过截屏的功能,有的时候截屏给我们带来了很多方便。那么我们怎么才能在Android上实现这个功能呢?下面我们就给大家提供一些代码用以实现Android上的截屏功能。

做掌上BT软件或者移动办公软件 ,一般都会有这样一个功能,用户可以对屏幕当前显示的数据或报表进行截图,并通过邮件发送。本文的开发实例就是要实现此功能。

其中有一个开发时候的小技巧:

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

下面k将实现方法跟大家分享一下:

  1. package com.johnson.Screenshot;     
  2. import java.io.File;     
  3. import java.io.FileNotFoundException;     
  4. import java.io.FileOutputStream;     
  5. import java.io.IOException;     
  6. import android.app.Activity;     
  7. import android.content.Context;     
  8. import android.content.Intent;     
  9. import android.graphics.Bitmap;     
  10. import android.graphics.Rect;     
  11. import android.net.Uri;     
  12. import android.os.Environment;     
  13. import android.os.StatFs;     
  14. import android.view.View;     
  15. import android.widget.Toast;     
  16. public class ScreenshotTools {     
  17.   /***    
  18.     * @author Johnson    
  19.     *       
  20.     * */     
  21.   public static long minSizeSDcard = 50;     
  22.   public static String filePath = Environment.getExternalStorageDirectory()     
  23.       + "/FJBICache";     
  24.   public static String fileName = "chart.png";     
  25.   public static String detailPath = filePath + File.separator + fileName;     
  26.   public static final int SEND_EMAIL = 1;     
  27.   // public static String detailPath="/sdcard/FjbiCache/chart.png";     
  28.   /**    
  29.     * 调用系统程序发送邮件    
  30.     *       
  31.     * @author Johnson    
  32.     *       
  33.     * */     
  34.   private static void sendEmail(Context context, String[] to, String subject,     
  35.       String body, String path) {     
  36.     Intent email = new Intent(android.content.Intent.ACTION_SEND);     
  37.     if (to != null) {     
  38.       email.putExtra(android.content.Intent.EXTRA_EMAIL, to);     
  39.     }     
  40.     if (subject != null) {     
  41.       email.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);     
  42.     }     
  43.     if (body != null) {     
  44.       email.putExtra(android.content.Intent.EXTRA_TEXT, body);     
  45.     }     
  46.     if (path != null) {     
  47.       /*    
  48.         * 用email.setType("image/png");或者email.setType(    
  49.         * "application/octet-stream"); 都不会影响邮件的发送    
  50.         * 为什么email.setType("image/png"    
  51.         * );而不用email.setType("application/octet-stream"); ?    
  52.         * 因为在开发中发现setType("image/png"),系统会同时给你调用彩信,邮件,等.....    
  53.         */     
  54.       File file = new File(path);     
  55.       email.putExtra(android.content.Intent.EXTRA_STREAM,     
  56.           Uri.fromFile(file));     
  57.       email.setType("image/png");     
  58.     }     
  59.     context.startActivity(Intent.createChooser(email, "请选择发送软件"));     
  60.   }     
  61.   /**    
  62.     * 获取指定Activity的截屏,保存到png文件    
  63.     *       
  64.     * @author Johnson    
  65.     * **/     
  66.   private static Bitmap takeScreenShot(Activity activity) {     
  67.     // View是你需要截图的View     
  68.     View view = activity.getWindow().getDecorView();     
  69.     view.setDrawingCacheEnabled(true);     
  70.     view.buildDrawingCache();     
  71.     Bitmap b1 = view.getDrawingCache();     
  72.     // 获取状态栏高度     
  73.     Rect frame = new Rect();    activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);     
  74.     int statusBarHeight = frame.top;     
  75.     System.out.println(statusBarHeight);     
  76.     // 获取屏幕长和高     
  77.     int width = activity.getWindowManager().getDefaultDisplay().getWidth();     
  78.     int height = activity.getWindowManager().getDefaultDisplay()     
  79.         .getHeight();     
  80.     // 去掉标题栏     
  81.     // Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);     
  82.     Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height     
  83.         - statusBarHeight);     
  84.     view.destroyDrawingCache();     
  85.     return b;     
  86.   }     
  87.   /**    
  88.     * 截图保存    
  89.     *       
  90.     * @author Johnson    
  91.     * **/     
  92.   private static void savePic(Bitmap b, String filePath, String fileName) {     
  93.     File f = new File(filePath);     
  94.     if (!f.exists()) {     
  95.       f.mkdir();     
  96.     }     
  97.     FileOutputStream fos = null;     
  98.     try {     
  99.       fos = new FileOutputStream(filePath + File.separator + fileName);     
  100.       if (null != fos) {     
  101.         b.compress(Bitmap.CompressFormat.PNG, 90, fos);     
  102.         fos.flush();     
  103.         fos.close();     
  104.       }     
  105.     } catch (FileNotFoundException e) {     
  106.       e.printStackTrace();     
  107.     } catch (IOException e) {     
  108.       e.printStackTrace();     
  109.     }     
  110.   }     
  111.   /**    
  112.     *       
  113.     * 截屏并发送邮件    
  114.     *       
  115.     * @author Johnson    
  116.     * **/     
  117.   public static void takeScreenShotToEmail(Context context, Activity a) {     
  118.     if (getAvailableSDcard(context)) {     
  119.       savePic(takeScreenShot(a), filePath, fileName);     
  120.       // selectDialog(context);     
  121.       sendEmail(context, nullnullnull, detailPath);     
  122.     }     
  123.   }     
  124.   /***    
  125.     * Sd判断SD卡是否可用    
  126.     *       
  127.     * @author Johnson minSizeSDcard>50kb    
  128.     * */     
  129.   public static boolean getAvailableSDcard(Context context) {     
  130.     boolean sdCardExist = Environment.getExternalStorageState().equals(     
  131.         android.os.Environment.MEDIA_MOUNTED); // 判断sd卡是否存在     
  132.     System.out.println("+++" + sdCardExist);     
  133.     if (sdCardExist) {     
  134.       File path = Environment.getExternalStorageDirectory();     
  135.       StatFs stat = new StatFs(path.getPath());     
  136.       long blockSize = stat.getBlockSize();     
  137.       long availableBlocks = stat.getAvailableBlocks();     
  138.       long sdCardSize = (availableBlocks * blockSize) / 1024;// KB值     
  139.       if (sdCardSize > minSizeSDcard) {     
  140.         System.out.println("SDcardSize:::" + minSizeSDcard + "KB");     
  141.         return true;     
  142.       } else {     
  143.         Toast.makeText(context, "SD卡空间不足", Toast.LENGTH_SHORT).show();     
  144.       }     
  145.     } else {     
  146.       Toast.makeText(context, "请在使用转发功能之前插入SD卡", Toast.LENGTH_SHORT)     
  147.           .show();     
  148.     }     
  149.     return false;     
  150.   }     
  151. }       
  152. package com.johnson.Screenshot;     
  153. import android.app.Activity;     
  154. import android.content.Context;     
  155. import android.os.Bundle;     
  156. import android.view.View;     
  157. import android.view.View.OnClickListener;     
  158. import android.widget.Button;     
  159. public class ScreenshotActivity extends Activity {     
  160.         /** Called when the activity is first created. */     
  161.   Button bt;     
  162.   Context mContext;     
  163.         @Override     
  164.         public void onCreate(Bundle savedInstanceState) {     
  165.                 super.onCreate(savedInstanceState);     
  166.                 setContentView(R.layout.main);     
  167.                 bt=(Button)findViewById(R.id.button1);     
  168.                 mContext=this;     
  169.                 bt.setOnClickListener(new OnClickListener() {     
  170.       @Override     
  171.       public void onClick(View v) {     
  172.         // TODO Auto-generated method stub     
  173.         ScreenshotTools.takeScreenShotToEmail(mContext, ScreenshotActivity.this);     
  174.       }     
  175.     });             
  176.         }     
  177. }   

XML/HTML代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>     
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     
  3.         android:layout_width="fill_parent"     
  4.         android:layout_height="fill_parent"     
  5.         android:orientation="vertical" >     
  6.         <Button     
  7.                 android:id="@+id/button1"     
  8.                 android:layout_width="wrap_content"     
  9.                 android:layout_height="wrap_content"     
  10.                 android:text="@string/button_text" />     
  11. </LinearLayout>  
责任编辑:闫佳明 来源: jizhuomi
相关推荐

2009-09-03 17:23:45

C#发送邮件

2014-12-31 14:40:53

cropper截取截图

2013-09-02 15:26:44

.NET开发邮件系统.Net

2010-01-27 18:06:03

Android短信发送

2011-08-30 14:47:53

UbuntuLookit

2009-06-09 08:57:08

微软Windows 7操作系统

2022-09-02 15:08:02

Python邮件发送

2012-03-07 14:37:03

JavaJavaMail

2011-08-02 11:30:41

iOS开发 邮件发送

2020-11-24 11:00:24

前端

2009-12-02 16:31:54

PHP发送邮件

2009-12-09 15:23:36

PHP mail()函

2011-10-31 09:35:50

2009-10-29 08:32:21

Ubuntu 9.10截图

2009-03-04 09:26:17

Kumo搜索引擎截图

2017-04-26 09:00:23

Python发送邮件脚本

2012-02-16 11:04:32

2009-03-26 08:47:55

Windows Mob微软移动OS

2021-02-09 21:17:28

Android安卓

2013-03-29 09:49:06

Android开发小功能实现
点赞
收藏

51CTO技术栈公众号