一分钟解决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>

如果想学习如何实现微信分享的话,可以去我的下一篇一分钟让你实现Android微信分享功能
到这里就结束了,希望能够帮到大家哦!IT需要爱与和平😊,最后请大家关注我,以及我的一分钟系列(Android篇和小程序篇)让你一分钟实现相应的功能,成功引起产品经理的注意袄。

  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值