Android实现截图和分享功能的代码
作者:kong濤 时间:2023-04-09 22:56:43
先给大家展示下效果图吧
直接上代码:
xml的布局:
<Button
android:id="@+id/btn_jp"
android:layout_marginTop="10dip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="截屏"
android:textColor="#ff999999" />
<Button
android:id="@+id/btn_share"
android:layout_marginTop="10dip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="分享"
android:textColor="#ff999999" />
activity的方法:
private String imagePath;
//截屏
btnJp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// image = ScreenShot.shoot(AddressSelecterActivity.this);
screenshot();
// Bitmap bitmap = getBitmapByView(scrollView);
// savePic(bitmap);
}
});
//分享
btnShare.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (imagePath != null){
Intent intent = new Intent(Intent.ACTION_SEND); // 启动分享发送的属性
File file = new File(imagePath);
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));// 分享的内容
intent.setType("image/*");// 分享发送的数据类型
Intent chooser = Intent.createChooser(intent, "Share screen shot");
if(intent.resolveActivity(getPackageManager()) != null){
startActivity(chooser);
}
} else {
Toast.makeText(AddressSelecterActivity.this, "先截屏,再分享", Toast.LENGTH_SHORT).show();
}
}
});
截取工具:
//截取屏幕的方法
private void screenshot() {
// 获取屏幕
View dView = getWindow().getDecorView();
dView.setDrawingCacheEnabled(true);
dView.buildDrawingCache();
Bitmap bmp = dView.getDrawingCache();
if (bmp != null)
{
try {
// 获取内置SD卡路径
String sdCardPath = Environment.getExternalStorageDirectory().getPath();
// 图片文件路径
imagePath = sdCardPath + File.separator + "screenshot.png";
File file = new File(imagePath);
FileOutputStream os = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 100, os);
os.flush();
os.close();
} catch (Exception e) {
}
}
}
总结
以上所述是小编给大家介绍的Android实现截图和分享功能的代码网站的支持!
来源:http://blog.csdn.net/kongty/article/details/75420874
标签:android,截图,分享
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
安卓Android6.0权限动态获取操作示例
2023-01-26 22:56:43
![](https://img.aspxhome.com/file/2023/7/93297_0s.jpg)
android开发教程之实现listview下拉刷新和上拉刷新效果
2022-07-29 20:42:42
C#微信公众号开发之消息处理
2023-11-10 01:10:53
![](https://img.aspxhome.com/file/2023/9/100169_0s.png)
深入了解c# 匿名类型
2022-04-08 22:38:21
java8 Stream大数据量List分批处理切割方式
2023-02-10 22:34:36
c#高效率导出多维表头excel的实例代码
2022-10-17 04:45:49
Java队列篇之实现数组模拟队列及可复用环形队列详解
2021-09-12 17:49:42
![](https://img.aspxhome.com/file/2023/1/118821_0s.png)
详细解读Android系统中的application标签
2021-07-10 02:29:46
小米Java程序员第二轮面试10个问题 你是否会被刷掉?
2023-11-13 09:00:29
![](https://img.aspxhome.com/file/2023/6/59026_0s.jpg)
Android 调用系统照相机拍照和录像
2023-10-30 05:40:35
Java解析XML格式数据的方法详解
2022-03-25 09:28:25
浅谈Java代理(jdk静态代理、动态代理和cglib动态代理)
2023-06-09 18:57:49
仅用5分钟极速入门Dubbo使用教程
2022-08-08 12:08:55
![](https://img.aspxhome.com/file/2023/3/83433_0s.png)
Android自带emoji表情的使用方法详解
2021-11-25 09:28:22
![](https://img.aspxhome.com/file/2023/7/94967_0s.png)
使用Gradle做Java代码质量检查的方法示例
2021-08-10 00:45:06
![](https://img.aspxhome.com/file/2023/5/84225_0s.jpg)
Android开发之对话框案例详解(五种对话框)
2021-08-07 11:37:47
Java使用iTextPDF生成PDF文件的实现方法
2023-10-15 10:44:13
![](https://img.aspxhome.com/file/2023/7/58687_0s.png)
解决C#中WebBrowser的DocumentCompleted事件不执行的实现方法
2021-07-07 22:59:06
C#多线程系列之线程通知
2023-09-13 17:08:03
![](https://img.aspxhome.com/file/2023/4/96304_0s.jpg)
解析libcurl在android下的移植、编译与测试
2023-01-24 01:19:44