当前位置: 首页 > news >正文

Android原生分享与指定app分享

什么是 Android 系统的原生分享呢,如下图所示

创建一个 Intent ,指定其 Action 为 Intent.ACTION_SEND,这表示要创建一个发送指定内容的行动。

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);

 指定需要发送的内容和类型。

// 比如发送文本形式的数据内容
// 指定发送的内容
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text");
// 指定发送内容的类型
sendIntent.setType("text/plain"); 
// 比如发送二进制文件数据流内容(比如图片、视频、音频文件等等)
// 指定发送的内容 (EXTRA_STREAM 对于文件 Uri )
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
// 指定发送内容的类型 (MIME type)
shareIntent.setType("image/jpeg");

向系统发送Activity,打开系统分享选择器,出现如上图所示界面。 

startActivity(Intent.createChooser(shareIntent, "Share"));

完整代码如下:


// 原生通用分享文本
public void shareText(String title, String text){Intent sendIntent = new Intent();sendIntent.setAction(Intent.ACTION_SEND);if (title.isEmpty()){title = "share";}sendIntent.putExtra(Intent.EXTRA_TEXT, text);sendIntent.setType("text/plain");startActivityForResult(Intent.createChooser(sendIntent, title), 80001);
}// 原生通用分享图片
public void shareImage(String title, String filePath){Intent sendIntent = new Intent();sendIntent.setAction(Intent.ACTION_SEND);if (title.isEmpty()){title = "share";}File file = new File(filePath);Uri uri = getFileUri(this, file);sendIntent.putExtra(Intent.EXTRA_STREAM, uri);sendIntent.setType("image/png");startActivityForResult(Intent.createChooser(sendIntent, title), 80002);
}public Uri getFileUri(Context context, File file){Uri uri;// 低版本直接用 Uri.fromFileif (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {uri = Uri.fromFile(file);}else {try {uri = FileProvider.getUriForFile(this, getPackageName0() + ".fileProvider", file);} catch (Exception e) {e.printStackTrace();uri = getImageContentUri(context, file);}if (uri == null) {uri = getImageContentUri(context, file);}}return uri;
}public Uri getImageContentUri(Context context, File imageFile) {String filePath = imageFile.getAbsolutePath();Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,new String[] { MediaStore.Images.Media._ID }, MediaStore.Images.Media.DATA + "=? ",new String[] { filePath }, null);if (cursor != null && cursor.moveToFirst()) {int id = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID));Uri baseUri = Uri.parse("content://media/external/images/media");return Uri.withAppendedPath(baseUri, "" + id);} else {if (imageFile.exists()) {ContentValues values = new ContentValues();values.put(MediaStore.Images.Media.DATA, filePath);return context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);} else {return null;}}
}

如果要分享到指定的app呢?


// 特定App 分享文本
public void shareTextByApp(String pkgName, String appName, String title, String text){if (!checkAppInstalled(this, pkgName)){Toast.makeText(getApplicationContext(), "You need install the " + appName + " first", Toast.LENGTH_LONG).show();return;}if (title.isEmpty()){title = "share";}Intent sendIntent = new Intent();sendIntent.setAction(Intent.ACTION_SEND);sendIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, title);sendIntent.putExtra(Intent.EXTRA_TEXT, text);sendIntent.setType("text/plain");sendIntent.setPackage(pkgName);startActivity(sendIntent);
}// 特定App 分享文本
public void shareImageByApp(String pkgName, String appName, String title, String filePath){if (!checkAppInstalled(this, pkgName)){Toast.makeText(getApplicationContext(), "You need install the " + appName + " first", Toast.LENGTH_LONG).show();return;}if (title.isEmpty()){title = "share";}Intent sendIntent = new Intent();sendIntent.setAction(Intent.ACTION_SEND);sendIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, title);File file = new File(filePath);Uri uri = getFileUri(this, file);sendIntent.putExtra(Intent.EXTRA_STREAM, uri);sendIntent.setType("image/png");sendIntent.setPackage(pkgName);startActivity(sendIntent);
}// 是否安装某app
public boolean checkAppInstalled(Context context, String pkgName) {try {context.getPackageManager().getPackageInfo(pkgName, 0);} catch (Exception x) {return false;}return true;
}
//例如 WhatsApp 的分享
shareTextByApp("com.whatsapp", "WhatsApp", title, content);//Facebook 的分享
shareImageByApp("com.facebook.katana", "Facebook", title, filePath);

http://www.lryc.cn/news/216687.html

相关文章:

  • ActiveMQ是什么?-九五小庞
  • <蓝桥杯软件赛>零基础备赛20周--第4周--杂题-1
  • Telnet/ssh/Serial远程工具WindTerm
  • 电脑技巧:台式机噪音非常大的几个原因以及解决办法
  • C++名称空间
  • Centos7扩容
  • react中ref的使用(useRef,forwardRef,useImperativeHandle,createRef)
  • 正点原子嵌入式linux驱动开发——Linux USB驱动
  • (四)docker:为mysql和java jar运行环境创建同一网络,容器互联
  • 【kafka】记一次kafka基于linux的原生命令的使用
  • C语言或C++结构体及其变量的创建方式汇总
  • mysql之基础语句
  • Sentinel 哨兵数据 更新下载地址 2023年11月
  • 动态路由协议OSPF项目部署(二)
  • winscp文件增量同步到linux服务器
  • 足足68个!Python函数合集请收好!
  • vue2.0 打包,nginx部署
  • 微服务架构之路1,服务如何拆分?使用微服务的注意事项?
  • 解决Mac电脑音乐显示歌名的乱码问题
  • 赢在电商设计!2024年最新电商设计实战技巧盘点
  • 约数之和 (普通快速幂求逆元做法)
  • 每日一题(LeetCode)----二分查找(三)
  • 使用 TensorFlow FasterRCNN 网络进行目标检测
  • 数据结构——顺序表(SeqList)
  • Uni-App 快捷登录
  • DbUtils + Druid 实现 JDBC 操作 --- 附BaseDao
  • css:元素居中整理水平居中、垂直居中、水平垂直居中
  • 从零开始的目标检测和关键点检测(二):训练一个Glue的RTMDet模型
  • React18新特性?
  • 筹码博弈K线长阳选股公式,穿越筹码密集区