产品需求:

  微信分享多图至好友,朋友圈。由于微信禁用了分享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. 异步编程新方式async/await

    一.前言 实际上对async/await并不是很陌生,早在阮大大的ES6教程里面就接触到了,但是一直处于理解并不熟练使用的状态,于是决定重新学习并且总结一下,写了这篇博文.如果文中有错误的地方还请各位 ...

  2. async-await和Promise的关系

    关于异步处理,ES5的回调使我们陷入地狱,ES6的Promise使我们脱离魔障,终于.ES7的async-await带我们走向光明.今天就来学习一下 async-await. 经常会看到有了 asyn ...

  3. 如何设计一个牛逼的API接口

    在日常开发中,总会接触到各种接口.前后端数据传输接口,第三方业务平台接口.一个平台的前后端数据传输接口一般都会在内网环境下通信,而且会使用安全框架,所以安全性可以得到很好的保护.这篇文章重点讨论一下提 ...

  4. 转 mysql show processlist 查看当前连接

    show processlist和show full processlist processlist命令的输出结果显示了有哪些线程在运行,不仅可以查看当前所有的连接数,还可以查看当前的连接状态帮助识别 ...

  5. go内建方法 append copy delete

    package mainimport "fmt"func main() { testAppend() testCopy() testDelete()}func testAppend ...

  6. ansible用authorized_key模块批量推送密钥到受控主机(免密登录)(ansible2.9.5)

    一,ansible的authorized_key模块的用途 用来配置密钥实现免密登录: ansible所在的主控机生成密钥后,如何把公钥上传到受控端? 当然可以用ssh-copy-id命令逐台手动处理 ...

  7. 千万不要往 Shell 里粘贴命令!

    对于用惯了 IDE 的程序员来说,在终端里敲命令可能没那么顺手,也记不住那么多复杂的命令.比较偷懒的做法就是网上搜相关的命令,复制到剪贴板往命令行窗口里一贴,完事! 但是这么做有很大的风险,为什么呢? ...

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

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

  9. 了解Js中的client,offset

    Client clientWidth,clientHeight 元素内部的宽度和高度,clientTop,clientLeft 元素内边距到其边框的距离,clientX,clientY相当于浏览器窗口 ...

  10. 【tensorflow】VMware Ubuntu+Tensorflow配置和使用

    本文主要是记录配置tf环境和虚拟机时遇到的问题和方法,方便日后再查找(补前三年欠下的技术债) 宿主机环境:win10 64位 宿主机python: anaconda+python3.6 宿主机tens ...