最近在做一个分享的页面,大家用的比较多的应该是友盟分享,当然那个需要第三方包,如果要求不高,当然是调用手机里自带的分享是最方便的,而且好用,话不多说,直接上代码
//系统自带的分享
public static void share(Activity activity,Bitmap bitmap){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/*");
Uri u = Uri.parse(MediaStore.Images.Media.insertImage(activity.getContentResolver(), bitmap, null,null));//将截图bitmap存系统相册
intent.putExtra(Intent.EXTRA_STREAM, u);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activity.startActivity(Intent.createChooser(intent, ""));
}
传入bitmap直接可以弹出分享窗啦,这里只能分享图片哦!
这里附带如何截图当前页面:截取以后,传入上面的分享中就可以分享出去了
//获取当前屏幕的大小
int width = getWindow().getDecorView().getRootView().getWidth();
int height = getWindow().getDecorView().getRootView().getHeight();
//生成相同大小的图片
Bitmap temBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
也可以将图片保存本地
/**
* 保存截屏至本地
*
* @param bitmap
*/
public void saveToLocal(Bitmap bitmap) {
//生成路径
String root = Environment.getExternalStorageDirectory().getAbsolutePath();
String dirName = "coinrise";
File appDir = new File(root, dirName);
if (!appDir.exists()) {
appDir.mkdirs();
}
//文件名为时间
long timeStamp = System.currentTimeMillis();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
String sd = sdf.format(new Date(timeStamp));
String fileName = sd + ".jpg";
//获取文件
File file = new File(appDir, fileName);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
if (fos != null) {
bitmap.compress(Bitmap.CompressFormat.JPEG, 50, fos);
fos.flush();
fos.close();
}
//通知系统相册刷新
XcjlActivity2.this.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
Uri.fromFile(new File(file.getPath()))));
T.showToastSafeOk("图片已保存至相册");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}