解决Android调用系统分享给微信,出现分享失败,分享多文件必须为图片格式的问题
作者:Huangzu98 发布时间:2023-07-03 11:08:04
标签:android,调用系统,分享微信
解决Android调用系统分享图片给微信,出现分享失败,分享多文件必须为图片格式
近期应公司需求,分享多图片到微信的功能,之前一直是用微信自己家SDK实现分享,但是查看微信的原生SDK是不具备多图分享的。在网上查找解决办法,直接调用手机系统进行分享,进行系统分享时分享给QQ,微博等都可以,但分享微信时就会出现分享失败,分享多文件必须为图片格式,看网上各路大神都各显神通都没解决具体问题,于是自己就总结出此篇文章为后来者少踩些坑,让你更快完成公司交给你的任务,让产品经理对你刮目相看,话不多说直接上干货。
private void systemShareWeChat(int shareTag,String photoPath){
Resources res=getResources();
Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.share);
File f = null;
ComponentName comp1,comp;
comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");//调用系统分享给微信朋友
comp1 = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");//调用系统分享给微信朋友圈
try {
//将Android中drawable图片保存到本地
String dir= Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"share"+".jpg";
f = new File(dir);
if (!f.exists()) {
f.getParentFile().mkdirs();
f.createNewFile();
}
FileOutputStream out = new FileOutputStream(f);
bmp.compress(Bitmap.CompressFormat.PNG, 80, out);
out.flush();
out.close();
Uri uri = FileProvider.getUriForFile(NativePhoto.this.getApplicationContext(),
"com.lipuwulian.blesample.provider", f);//这个是版本大于Android7.0(包含)临时访问文件,没有这个会报异常
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace(); }
ArrayList<Uri> imageUris = new ArrayList<Uri>();
imageUris.add(UrigetImageContentUri(NativePhoto.this,new File(photoPath)));//这个是分享本地存储的图片
imageUris.add(UrigetImageContentUri(NativePhoto.this,f));
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
if(shareTag==0){
shareIntent.setComponent(comp1);//分享给微信朋友圈
}else if(shareTag==1){
shareIntent.setComponent(comp);//分享给微信朋友
}
//如果去掉shareIntent.setComponent("*");系统会调出所有的分享软件
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
shareIntent.setType("image/*");
startActivity(shareIntent);
}
//如果是微信分享的话一定一定将这个直接复制到自己项目中,将自己图片路径换为content:不然就会出现上述错误
public static Uri UrigetImageContentUri(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);
Uri uri =null;
if (cursor !=null) {
if (cursor.moveToFirst()) {
int id = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID));
Uri baseUri = Uri.parse("content://media/external/images/media");
uri = Uri.withAppendedPath(baseUri, "" + id);
}
cursor.close();
}
if (uri ==null) {
ContentValues values =new ContentValues();
values.put(MediaStore.Images.Media.DATA, filePath);
uri = context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
}
return uri;
}
这样就解决了调用系统分享报出分享失败,分享多文件必须为图片格式的错误了。
在AndroidManifest中临时文件注册解决Android7.0版本及其之后文件uri报错问题
<application
android:allowBackup="true"
android:icon="@drawable/logo"
android:label="@string/app_name1"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<!-- //临时访问文件的注册-->
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.lipuwulian.blesample.provider"//这是自己的包名加“.provider”
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
<activity android:name="com.lipuwulian.blesample.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
在res文件夹下创建xml文件夹、
file_paths文件里的内容:path是data/包名加.testapplication/
<?xml version="1.0" encoding="utf-8"?>
<resources>
<paths>
<external-path
name="files_root"
path="Android/data/com.lipuwulian.blesample.testapplication/" />
<external-path
name="external_storage_root"
path="." />
</paths>
</resources>
到这里就结束了,希望能够帮到大家哦!IT需要爱与和平😊
来源:https://blog.csdn.net/qq_42754787/article/details/108509027


猜你喜欢
- bootstrap.yml和bootstrap.properties优先级直接先说结论 bootstrap.properties 优于boo
- 创建AlertDialog的步骤:1、创建AlertDialog.Builder对象2、调用Builder对象的setTitle
- 本文实例讲述了C#通过链表实现队列的方法。分享给大家供大家参考。具体实现方法如下:public class Node{ public int
- switchJava7开始,switch的参数可以是String类型了,这真的是一个很有用的改进,毕竟string还是挺常用的。到目前为止,
- 使用方法 首先在Github或者Gitee上面新建一个仓库复制仓库的链接用idea在本地新建一个demo项目点击菜单栏的VCS,按
- 本文实例讲述了Android7.0开发实现Launcher3去掉应用抽屉的方法。分享给大家供大家参考,具体如下:年初做过一个项目,有一个需求
- 本文实例为大家分享了android水平循环滚动控件的具体代码,供大家参考,具体内容如下CycleScrollView.javapackage
- 简介记录一个利用系统分享功能进行图片分享的工具类(代码是用Kotlin写的,都是比较简单的语法,部分可能需要自定义的地方都已经标出)。调用方
- java.lang.ArrayStoreException 分析这个demo来说明怎样排查一个spring boot 1应用升级到sprin
- 如下面代码以交灯为示例:public class Test { public static
- 本文实例为大家分享了java商品库存管理平台的具体代码,供大家参考,具体内容如下1.完成超市商品初始化。创建商品,将商品添加到集合2.显示来
- 从英文中重建数字给你一个字符串 s ,其中包含字母顺序打乱的用英文单词表示的若干数字(0-9)。按 升序 返回原始的数字。示例 1:输入:s
- Java读取txt文件内容。可以作如下理解:首先获得一个文件句柄。File file = new File(); file即为文件句柄。两人
- Semaphore 通常用于限制可以访问某些资源(物理或逻辑的)的线程数目。自从5.0开始,jdk在java.util.concurrent
- 在开发中当程序发生ANR或者异常,我们会将信息存在本地,然后上传服务器,这样可以实时去发现问题修改问题。那我们需要获取文件之后需要对文件进行
- 本文实例讲述了Java权重随机的实现方法。分享给大家供大家参考。具体分析如下:权重随机在项目中经常用到,所以我把它抽象到一个工具类中。一般实
- 背包问题主要是指一个给定容量的背包、若干具有一定价值和重量的物品,如何选择物品放入背包使物品的价值最大。其中又分01背包和无限背包,这里主要
- 下面通过一段内容有文字说明有代码分析,并附有展示图供大家学习。要解析HTTP报文,需要实现以下操作:读取HTTP报头提供的各种属性分析属性值
- MVP(Model-View-Presenter) 是总所周知MVC模式的一个演变,他们的主要目的都是划分模块职责,降低模块耦合,易测试,提
- Spring AOP底层原理代理模式一、什么是 AOPAOP 就是面向切面编程,是 OOP(面向对象编程)的延续。利用 AOP 可以对业务逻