产品需求:

  微信分享多图至好友,朋友圈。由于微信禁用了分享9图至朋友圈功能,这里分享微信只是将图片保存至本地,具体让用户手动分享。

问题分析:

  微信没有提供分享多图的SDK,因此我们实现调用系统自带的分享功能。将分享的图片保存至本地,再调用系统本地的分享实现分享多图操作。

具体实现:

  这里保存图片实现用了两种方式:

  1. 使用网络请求下载图片。
  2. 使用Glide加载图片。

    2.1 将请求网络的图片转化成bitmap,再将bitmap图片保存至本地相册。

    2.2 获取缓存至本地的文件,再将文件保存至指定目录,更新相册。

   注意:保存图片至系统相册目录时,只能在主线程实现。推断里面使用了IO流的原因。

一、分享多图至微信

 AtomicInteger count = new AtomicInteger();
ArrayList<Observable<File>> observables = new ArrayList<>();
List<Uri> imageUris = new ArrayList<Uri>();
mCommitDialog = WeiboDialogUtils.createLoadingDialog(this, "正在加载...");
observables.add(Observable.fromIterable(imageList).flatMap(new Function<String, ObservableSource<File>>() {
@Override
public ObservableSource<File> apply(String imagePath) throws Exception {
return Observable.create(new ObservableOnSubscribe<File>() {
@Override
public void subscribe(ObservableEmitter<File> emitter) throws Exception {
File file = PhotoUtils.saveImageToSdCard(getActivity(), imagePath);
emitter.onNext(file);
}
});
}
}).subscribeOn(Schedulers.io())); Observable.merge(observables).map(new Function<File, Boolean>() {
@Override
public Boolean apply(File f) throws Exception {
File file = new File(f.getAbsolutePath());
if(file.exists()) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {//android 7.0以下
imageUris.add(Uri.fromFile(file));
} else {//android 7.0及以上
Uri uri = Uri.parse(android.provider.MediaStore.Images.Media.insertImage(getContentResolver(), file.getAbsolutePath(), file.getName(), null));
imageUris.add(uri);
}
return true;
}
return false;
}}).observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<Boolean>() {
@Override
public void accept(Boolean a) throws Exception {
if(a) count.addAndGet(1);
if(imageList.size() == count.get()){
mCommitDialog.dismiss();
// showMessage(R.string.text_share_success );
//分享到微信好友
Intent intent = new Intent();
ComponentName componentName = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");
if (imageUris.size() == 0) return;
intent.setComponent(componentName);
intent.setAction(Intent.ACTION_SEND_MULTIPLE);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_STREAM, (Serializable) imageUris);
startActivity(intent);
} }
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
mCommitDialog.dismiss();
showMessage(R.string.text_save_failed);
}
}, new Action() {
@Override
public void run() throws Exception { }
})
;

saveImageToSdCard方法如下。

 //根据网络图片url路径保存到本地
public static final File saveImageToSdCard(Context context, String image) {
boolean success = false;
File file = null;
try {
file = createStableImageFile(context);
Bitmap bitmap = null;
URL url = new URL(image);
HttpURLConnection conn = null;
conn = (HttpURLConnection) url.openConnection();
InputStream is = null;
is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
FileOutputStream outStream;
outStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
success = true;
} catch (Exception e) {
e.printStackTrace();
} if (success) {
return file;
} else {
return null;
}
}

二、保存多图至本地。

  1.Glide加载图片生成Bitmap对象。

AtomicInteger count = new AtomicInteger();
ArrayList<Observable<Boolean>> observables = new ArrayList<>();
for (String imagePath :
imageList) {
observables.add(Observable.create(new ObservableOnSubscribe<Boolean>() {
@Override
public void subscribe(ObservableEmitter<Boolean> emitter) throws Exception {
SimpleTarget<Bitmap> into = Glide.with(getActivity())
.asBitmap()
.load(imagePath)
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady( Bitmap bitmap, @Nullable Transition<? super Bitmap> transition) {
if (bitmap != null) {
//图片信息不为空时才保存
emitter.onNext( PhotoUtils.savePhoto(getActivity(), DateUtils.formatDate(getActivity(),
System.currentTimeMillis()) + UUID.randomUUID() + ".png", bitmap));
}
}
});
}
}).subscribeOn(AndroidSchedulers.mainThread()));}
Observable.merge(observables).observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<Boolean>() {
@Override
public void accept(Boolean b) throws Exception {
if (b) {
count.addAndGet(1);
}
if(imageList.size() == count.get()){
showHintDialog();
// showMessage(R.string.text_save_pic_success );
} }
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
showMessage(R.string.text_save_pic_failed);
}
}, new Action() {
@Override
public void run() throws Exception { }
});

  2.Glide加载图片,得到文件形式。

File file = Glide.with(context)
.load(images[i])
.downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
.get()

savePhoto方法如下:

/**
* 保存图片
*/
public static boolean savePhoto(Context context, String fileName, Bitmap bitmap) {
//系统相册目录
String galleryPath = Environment.getExternalStorageDirectory()
+ File.separator + Environment.DIRECTORY_DCIM
+ File.separator + "Camera" + File.separator;
File imagePath = new File(galleryPath);
if (!imagePath.exists()) {
imagePath.mkdirs();
}    
File fileUri = new File(imagePath + File.separator + fileName);
if (!fileUri.exists()) {
try {
fileUri.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
Uri imgUri = Uri.fromFile(fileUri); try {
FileOutputStream out = new FileOutputStream(fileUri);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(imgUri);
context.sendBroadcast(intent);
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} return false;
}

注意:这里要申请权限。

参考博客:

1.APP分享多张图片和文字到微信朋友圈(android 7.0以上适配)

2.Android保存多张图片到本地

APP分享多张图片到微信和朋友圈的更多相关文章

  1. [h5+api]移动app开发用到的微信好友,朋友圈,qq好友,新浪微博分享合集

    适用H5+环境,能够使用plus方法的移动app中 /** * Created by HBuilder. * User: tyx * Date: 2018-11-21 * Time: 17:28:51 ...

  2. Android 调用系统分享文字、图片、文件,可直达微信、朋友圈、QQ、QQ空间、微博

    原文:Android 调用系统分享文字.图片.文件,可直达微信.朋友圈.QQ.QQ空间.微博 兼容SDK 18以上的系统,直接调用系统分享功能,分享文本.图片.文件到第三方APP,如:微信.QQ.微博 ...

  3. H5+ 分享到微信、朋友圈代码示例

    h5+分享到微信.朋友圈代码示例 在使用分享功能的时候会莫名的分享失败,debug时发现是图片过大的问题. 图片过大时ios平台上返回错误码-8,安卓上返回错误码-3(我测试是这样) 因此如果第一次分 ...

  4. Android分享到微信和朋友圈的工具类

    1.只要填写上正确的app_id,且引用上该工具类你就能实现分享到朋友圈和分享到微信. 2.需要到微信平台下载jar包,以及注册一个appid import android.content.Conte ...

  5. Apple Watch版微信来了 收发微信刷朋友圈不在话下

    昨晚果粉守了一夜的Apple Watch发布会,意料中的惊喜不少,最让人兴奋的是微信成为首批支持的应用.是的,在全球拥有4.68亿月活跃用户的微信怎么可能不第一时间入驻呢?之前我们就有聊过Apple ...

  6. 在小程序内点击按钮分享H5网页给好友或者朋友圈

    在小程序内点击按钮分享H5网页给好友或者朋友圈 首先需要建立h5容器文件夹 页面.wxml <navigator url="/pages/report-await/fouryearh5 ...

  7. H5分享到微信好友朋友圈QQ好友QQ空间微博二维码

    这是分享按钮: <button onclick="call()">通用分享</button> <button onclick="call(' ...

  8. Android 分享微信好友 朋友圈

    第三方应用,可以调用微信分享,把链接,文字,各种media,分享到微信好友或者微信朋友圈,步骤: package com.edaixi.utils; import android.content.Co ...

  9. 关于APP分享到QQ、微信等

    <script> var shares=null;        var Intent=null,File=null,Uri=null,main=null; function plusRe ...

随机推荐

  1. 霍夫曼编码(Huffman)

    题目:有一个字符串:cabcedeacacdeddaaaba,问题: (1)采用霍夫曼编码画出编码的过程,并写出各字符的编码 (2)根据求得的编码,求得各编码需要的总位数 (3)求出整个字符串总编码长 ...

  2. day42 Pyhton 并发编程05

    一.内容回顾 # 线程 # 正常的编程界: # 进程 # 计算机中最小的资源分配单位 # 数据隔离 # 进程可以独立存在 # 创建与销毁 还有切换 都慢 给操作系统压力大 # 线程 # 计算机中能被C ...

  3. 干货分享:在Windows下使用Visual Studio搭建C语言开发环境

    前言:本文将教大家如何使用 VIsual Studio Code 搭建 C 语言开发环境,包括使用 VS Code 如何编译和调试 C 语言程序,需要 用到的工具有 Visual Studio Cod ...

  4. D - 活动选择

    Description 学校的大学生艺术中心周日将面向全校各个学院的学生社团开放,但活动中心同时只能供一个社团活动使用,并且每一个社团活动开始后都不能中断.现在各个社团都提交了他们使用该中心的活动计划 ...

  5. Docker学习笔记之-通过Xshell连接 CentOS服务

    上一节演示如何在虚拟机中安装 CentOS服务,Docker学习笔记之-在虚拟机VM上安装CentOS 7.8 本节主要演示如何通过 Xshell软件链接CentOS服务,本例以虚拟机作为演示,直接在 ...

  6. spring-boot-route(二十三)开发微信公众号

    在讲微信公众号开发之前,先来大概了解一下微信公众号.微信公众号大体上可以分为服务号和订阅号,订阅号和服务号的区别如下: 服务号可以申请微信支付功能. 服务号只能由企业申请,订阅号可以有企业或个人申请. ...

  7. deployer服务器端的配置

    虽然说是无侵入的部署工具,但是还是需要我们来做一些微小的配置的,因为出于安全考虑,我们一般不会开发 root 用户的 SSH 登录,而是使用其它用户,比如 Ubuntu 默认的 ubuntu 用户.我 ...

  8. D. Kilani and the Game 解析(裸BFS、實作)

    Codeforce 1105 D. Kilani and the Game 解析(裸BFS.實作) 今天我們來看看CF1105D 題目連結 題目 給一個\(n\times m\)的地圖,地圖上有幾種格 ...

  9. DTU有哪些功能特点?

    DTU指的是一种数据终端设备(Data Terminal unit),可以把它简单理解为下位GPRS发射终端,在进行通信的时候,传输数据的链路两端肯定是存在DTU,所传信息进行格式转换和数据整理效验都 ...

  10. Redis常用命令(2)——String

    APPEND 格式:APPEND key value 作用:在key的键值后追加value,如果key不存在,则创建key,并存入value. 返回值:追加value后的字符串长度. 示例: 192. ...