做掌上BT軟件或者移動辦公軟件 ,一般都會有這樣一個功能,用戶可以對屏幕當前顯示的數據或報表進行截圖,並通過郵件發送。本文的開發實例就是要實現此功能。
其中有一個開發時候的小技巧:
用email.setType("image/png");或者email.setType("application/octet-stream"); 都不會影響郵件的發送。為什麼email.setType("image/png");而不用email.setType("application/octet-stream"); ? 因為在開發中發現setType("image/png"),系統會同時給你調用彩信,郵件,等.....
下面將實現方法跟大家分享一下:
Java代碼
- package com.johnson.Screenshot;
-
- 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;
- }
-
- }
-
-
- package com.johnson.Screenshot;
-
- 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);
- }
- });
-
-
-
- }
- }
XML/HTML代碼
- <?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>