|
@@ -0,0 +1,298 @@
|
|
|
|
+package com.cooleshow.chatmodule.utils.helper;
|
|
|
|
+
|
|
|
|
+import android.app.Activity;
|
|
|
|
+import android.content.Context;
|
|
|
|
+import android.content.Intent;
|
|
|
|
+import android.graphics.Bitmap;
|
|
|
|
+import android.graphics.BitmapFactory;
|
|
|
|
+import android.net.Uri;
|
|
|
|
+import android.provider.MediaStore;
|
|
|
|
+import android.text.TextUtils;
|
|
|
|
+import android.util.Base64;
|
|
|
|
+import android.util.Log;
|
|
|
|
+
|
|
|
|
+import com.cooleshow.base.utils.FileUtils;
|
|
|
|
+import com.cooleshow.base.utils.MyFileUtils;
|
|
|
|
+import com.cooleshow.base.utils.ToastUtil;
|
|
|
|
+import com.cooleshow.base.utils.Utils;
|
|
|
|
+import com.cooleshow.chatmodule.manager.IMCenter;
|
|
|
|
+import com.tencent.imsdk.v2.V2TIMMessage;
|
|
|
|
+import com.tencent.imsdk.v2.V2TIMSendCallback;
|
|
|
|
+import com.umeng.socialize.ShareAction;
|
|
|
|
+import com.umeng.socialize.UMShareAPI;
|
|
|
|
+import com.umeng.socialize.UMShareListener;
|
|
|
|
+import com.umeng.socialize.bean.SHARE_MEDIA;
|
|
|
|
+import com.umeng.socialize.media.UMImage;
|
|
|
|
+
|
|
|
|
+import org.json.JSONException;
|
|
|
|
+import org.json.JSONObject;
|
|
|
|
+
|
|
|
|
+import java.io.File;
|
|
|
|
+import java.io.FileNotFoundException;
|
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
|
+import java.util.Date;
|
|
|
|
+import java.util.Locale;
|
|
|
|
+
|
|
|
|
+import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
|
|
|
|
+import io.reactivex.rxjava3.core.Observable;
|
|
|
|
+import io.reactivex.rxjava3.core.ObservableEmitter;
|
|
|
|
+import io.reactivex.rxjava3.core.ObservableOnSubscribe;
|
|
|
|
+import io.reactivex.rxjava3.core.Observer;
|
|
|
|
+import io.reactivex.rxjava3.disposables.Disposable;
|
|
|
|
+import io.reactivex.rxjava3.schedulers.Schedulers;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Author by pq, Date on 2022/5/31.
|
|
|
|
+ */
|
|
|
|
+public class IMShareHelper {
|
|
|
|
+ public static final String WECHAT_TAG = "wechat";
|
|
|
|
+ public static final String WECHAT_CIRCLE_TAG = "wechat_circle";
|
|
|
|
+ public static final String SINA_TAG = "sina";
|
|
|
|
+ public static final String IMAGE_TAG = "image";
|
|
|
|
+ public static final String VIDEO_TAG = "video";
|
|
|
|
+
|
|
|
|
+ public static void saveImg(Context context, String base64, ResultCallBack resultCallBack) {
|
|
|
|
+ Observable.create(new ObservableOnSubscribe<File>() {
|
|
|
|
+ @Override
|
|
|
|
+ public void subscribe(ObservableEmitter<File> emitter) throws Exception {
|
|
|
|
+ File file = saveImgToLocalFile(base64);
|
|
|
|
+ emitter.onNext(file);
|
|
|
|
+ }
|
|
|
|
+ }).subscribeOn(Schedulers.newThread())
|
|
|
|
+ .observeOn(AndroidSchedulers.mainThread())
|
|
|
|
+ .subscribe(new Observer<File>() {
|
|
|
|
+ @Override
|
|
|
|
+ public void onSubscribe(Disposable d) {
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void onNext(File file) {
|
|
|
|
+ if (context != null) {
|
|
|
|
+ try {
|
|
|
|
+ MediaStore.Images.Media.insertImage(context.getContentResolver(),
|
|
|
|
+ file.getAbsolutePath(), file.getName(), null);
|
|
|
|
+ // 最后通知图库更新
|
|
|
|
+ context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
|
|
|
|
+ Uri.fromFile(new File(file.getPath()))));
|
|
|
|
+ if (resultCallBack != null) {
|
|
|
|
+ resultCallBack.onResult(true);
|
|
|
|
+ }
|
|
|
|
+ } catch (FileNotFoundException e) {
|
|
|
|
+ ToastUtil.getInstance().show(context, "保存失败");
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void onError(Throwable e) {
|
|
|
|
+ if (resultCallBack != null) {
|
|
|
|
+ resultCallBack.onResult(false);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void onComplete() {
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static void shareImgToChatGroup(Bitmap bitmap, String targetId, boolean isGroup, ResultCallBack resultCallBack) {
|
|
|
|
+ Observable.create(new ObservableOnSubscribe<String>() {
|
|
|
|
+ @Override
|
|
|
|
+ public void subscribe(ObservableEmitter<String> emitter) throws Exception {
|
|
|
|
+ File file = saveImgToLocalFile(bitmap);
|
|
|
|
+ toShare(file, targetId, isGroup, resultCallBack);
|
|
|
|
+ emitter.onNext("");
|
|
|
|
+ }
|
|
|
|
+ }).subscribeOn(Schedulers.io())
|
|
|
|
+ .observeOn(AndroidSchedulers.mainThread())
|
|
|
|
+ .subscribe(new Observer<String>() {
|
|
|
|
+ @Override
|
|
|
|
+ public void onSubscribe(Disposable d) {
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void onNext(String s) {
|
|
|
|
+ if (resultCallBack != null) {
|
|
|
|
+ resultCallBack.onResult(true);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void onError(Throwable e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ if (resultCallBack != null) {
|
|
|
|
+ resultCallBack.onResult(false);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void onComplete() {
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static void shareImgToChatGroup(String base64, String targetId, boolean isGroup, ResultCallBack resultCallBack) {
|
|
|
|
+ Observable.create(new ObservableOnSubscribe<String>() {
|
|
|
|
+ @Override
|
|
|
|
+ public void subscribe(ObservableEmitter<String> emitter) throws Exception {
|
|
|
|
+ File file = saveImgToLocalFile(base64);
|
|
|
|
+ toShare(file, targetId, isGroup, resultCallBack);
|
|
|
|
+ emitter.onNext("");
|
|
|
|
+ }
|
|
|
|
+ }).subscribeOn(Schedulers.io())
|
|
|
|
+ .observeOn(AndroidSchedulers.mainThread())
|
|
|
|
+ .subscribe(new Observer<String>() {
|
|
|
|
+ @Override
|
|
|
|
+ public void onSubscribe(Disposable d) {
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void onNext(String s) {
|
|
|
|
+ if (resultCallBack != null) {
|
|
|
|
+ resultCallBack.onResult(true);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void onError(Throwable e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ if (resultCallBack != null) {
|
|
|
|
+ resultCallBack.onResult(false);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void onComplete() {
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static void toShare(File file, String targetId, boolean isGroup, ResultCallBack resultCallBack) {
|
|
|
|
+ IMCenter.getInstance().sendImageMessage(targetId, isGroup, file.getAbsolutePath(), new V2TIMSendCallback<V2TIMMessage>() {
|
|
|
|
+ @Override
|
|
|
|
+ public void onProgress(int i) {
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void onSuccess(V2TIMMessage v2TIMMessage) {
|
|
|
|
+ Log.i("pq", "message send success");
|
|
|
|
+ if (resultCallBack != null) {
|
|
|
|
+ resultCallBack.onResult(true);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void onError(int i, String s) {
|
|
|
|
+ Log.i("pq", "message send onError:" + i);
|
|
|
|
+ if (resultCallBack != null) {
|
|
|
|
+ resultCallBack.onResult(false);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static File saveImgToLocalFile(String base64) {
|
|
|
|
+ byte[] bytes;
|
|
|
|
+ if (base64.startsWith("data:image/png;base64")) {
|
|
|
|
+ String[] split = base64.split(",");
|
|
|
|
+ bytes = Base64.decode(split[1], Base64.DEFAULT);
|
|
|
|
+ } else {
|
|
|
|
+ bytes = Base64.decode(base64, Base64.DEFAULT);
|
|
|
|
+ }
|
|
|
|
+ Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
|
|
|
|
+ File parentFile = new File(FileUtils.getCacheDir(Utils.getApp()) + File.separator + "share");
|
|
|
|
+ if (!parentFile.exists()) {
|
|
|
|
+ parentFile.mkdirs();
|
|
|
|
+ }
|
|
|
|
+ String targetFileName = "IMG_" + new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.CHINA).format(new Date()) + ".png";
|
|
|
|
+ File file = new File(parentFile, targetFileName);
|
|
|
|
+ FileUtils.saveImageToLocal(bitmap, file.getAbsolutePath());
|
|
|
|
+ return new File(file.getAbsolutePath());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static File saveImgToLocalFile(Bitmap bitmap) {
|
|
|
|
+ File parentFile = new File(FileUtils.getCacheDir(Utils.getApp()) + File.separator + "share");
|
|
|
|
+ if (!parentFile.exists()) {
|
|
|
|
+ parentFile.mkdirs();
|
|
|
|
+ }
|
|
|
|
+ String targetFileName = "IMG_" + new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.CHINA).format(new Date()) + ".png";
|
|
|
|
+ File file = new File(parentFile, targetFileName);
|
|
|
|
+ FileUtils.saveImageToLocal(bitmap, file.getAbsolutePath());
|
|
|
|
+ return new File(file.getAbsolutePath());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static void parseShareContactData(String base64, Intent data) {
|
|
|
|
+ String targetId = data.getStringExtra("targetId");
|
|
|
|
+ int conversationValue = data.getIntExtra("conversation", -1);
|
|
|
|
+ if (!TextUtils.isEmpty(base64)) {
|
|
|
|
+ //沿用之前的conversation=3为group
|
|
|
|
+ shareImgToChatGroup(base64, targetId, conversationValue ==3, new ResultCallBack() {
|
|
|
|
+ @Override
|
|
|
|
+ public void onResult(boolean isSuccess) {
|
|
|
|
+ if (isSuccess) {
|
|
|
|
+ ToastUtil.getInstance().showShort("分享成功");
|
|
|
|
+ } else {
|
|
|
|
+ ToastUtil.getInstance().showShort("分享失败");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static void parseShareActivity(Activity activity, JSONObject jsonObject, UMShareListener shareListener) {
|
|
|
|
+ JSONObject content = null;
|
|
|
|
+ try {
|
|
|
|
+ content = jsonObject.getJSONObject("content");
|
|
|
|
+ String shareTitle = content.getString("title");
|
|
|
|
+ String shareDesc = content.getString("desc");
|
|
|
|
+ String type = content.getString("type");
|
|
|
|
+ String shareType = content.getString("shareType");
|
|
|
|
+ SHARE_MEDIA share_media = SHARE_MEDIA.WEIXIN;
|
|
|
|
+ if (TextUtils.equals(shareType, WECHAT_TAG)) {
|
|
|
|
+ //分享微信
|
|
|
|
+ share_media = SHARE_MEDIA.WEIXIN;
|
|
|
|
+ }
|
|
|
|
+ if (TextUtils.equals(shareType, WECHAT_CIRCLE_TAG)) {
|
|
|
|
+ //分享朋友圈
|
|
|
|
+ share_media = SHARE_MEDIA.WEIXIN_CIRCLE;
|
|
|
|
+ }
|
|
|
|
+ if (TextUtils.equals(shareType, SINA_TAG)) {
|
|
|
|
+ //分享新浪微博
|
|
|
|
+ share_media = SHARE_MEDIA.SINA;
|
|
|
|
+ }
|
|
|
|
+ if (!UMShareAPI.get(Utils.getApp()).isInstall(activity, share_media)) {
|
|
|
|
+ ToastUtil.getInstance().show(Utils.getApp(), "应用未安装,分享失败");
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ if (TextUtils.equals(type, IMAGE_TAG)) {
|
|
|
|
+ //分享图片
|
|
|
|
+ String imageData = content.getString("image");
|
|
|
|
+ UMImage image = new UMImage(activity, MyFileUtils.base64ToBitmap(imageData.split(",")[1]));//bitmap文件
|
|
|
|
+ image.setThumb(image);
|
|
|
|
+ image.compressFormat = Bitmap.CompressFormat.PNG;
|
|
|
|
+ image.compressStyle = UMImage.CompressStyle.SCALE;
|
|
|
|
+ image.setTitle(shareTitle);
|
|
|
|
+ image.setDescription(shareDesc);
|
|
|
|
+ new ShareAction(activity).withMedia(image)
|
|
|
|
+ .setPlatform(share_media)
|
|
|
|
+ .setCallback(shareListener)
|
|
|
|
+ .share();
|
|
|
|
+ }
|
|
|
|
+ } catch (JSONException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public interface ResultCallBack {
|
|
|
|
+ void onResult(boolean isSuccess);
|
|
|
|
+ }
|
|
|
|
+}
|