android屏幕截图并通过邮件发送
(2012-08-12 13:24:44)
标签:
杂谈
if (path != null) {
File file = new File(path);
email.putExtra(android.content.Intent.EXTRA_STREAM, Uri
.fromFile(file));
email.setType("image/png");
}else{
// email.setType("application/octet-stream");
}
还有读写权限:
android:name="android.permission.WRITE_EXTERNAL_STORAGE">
package fjbi.ui;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
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 Tools {
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";
// 获取指定Activity的截屏,保存到png文件
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;
}
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();
}
}
public static void takeScreenShotToEmail(Context context, Activity
a) {
if (getAvailableSDcard(context)) {
savePic(takeScreenShot(a), filePath, fileName);
// selectDialog(context);
sendEmail(context, null, null, null, detailPath);
}
}
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 ts = new Toast(context);
// ts.setText("SD卡空间不足");
// ts.show();
// Toast.makeText(context.getApplicationContext(), "SD卡空间不足",
// Toast.LENGTH_SHORT).show();
Toast.makeText(context, "SD卡空间不足",
Toast.LENGTH_SHORT).show();
}
} else {
// Toast ts = new Toast(context);
// ts.setText("SD卡不存在");
// ts.show();
// Toast.makeText(context.getApplicationContext(), "SD卡不存在",
// Toast.LENGTH_SHORT).show();
Toast.makeText(context, "请在使用转发功能之前插入SD卡",
Toast.LENGTH_SHORT)
.show();
}
return false;
}
private static void sendEmail(Context context, String[] to, String
subject,
String body, String path) {
// to = new String[] { "411148686@qq.com" };
// subject = "你有一条短信";
// body = "我要发送邮件啊";
// path=filePath+fileName;
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) {
File file = new File(path);
email.putExtra(android.content.Intent.EXTRA_STREAM, Uri
.fromFile(file));
email.setType("image/png");
}else{
//email.setType("application/octet-stream");
}
context.startActivity(Intent.createChooser(email,
"请选择发送软件"));
}
public static void feedbackEmail(Context context) {
String str =
context.getResources().getString(R.string.feedback_email);
String[] to = new String[] { str };
sendEmail(context, to, null, null, null);
}
}
分享:
喜欢
0
赠金笔
加载中,请稍候......
评论加载中,请稍候...
发评论
登录名: 密码: 找回密码 注册记住登录状态
昵 称:
评论并转载此博文
发评论
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。