|
@@ -0,0 +1,282 @@
|
|
|
+package com.cooleshow.base.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 com.cooleshow.base.bean.ShareIntentBean;
|
|
|
+import com.cooleshow.base.utils.AppUtils;
|
|
|
+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.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 com.umeng.socialize.media.UMWeb;
|
|
|
+
|
|
|
+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 CommonShareHelper {
|
|
|
+ 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 final String LINK_TAG = "link";
|
|
|
+
|
|
|
+ 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 saveImg(Context context, Bitmap bitmap, ResultCallBack resultCallBack) {
|
|
|
+ Observable.create(new ObservableOnSubscribe<File>() {
|
|
|
+ @Override
|
|
|
+ public void subscribe(ObservableEmitter<File> emitter) throws Exception {
|
|
|
+ File file = saveImgToLocalFile(bitmap);
|
|
|
+ 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() {
|
|
|
+
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ 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());
|
|
|
+ }
|
|
|
+
|
|
|
+ public 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 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();
|
|
|
+ }
|
|
|
+
|
|
|
+ if (TextUtils.equals(type, VIDEO_TAG)) {
|
|
|
+ String videoUrl = content.optString("video");
|
|
|
+ UMWeb video = new UMWeb(videoUrl);
|
|
|
+ video.setTitle(shareTitle);
|
|
|
+ video.setDescription(shareDesc.isEmpty() ? shareTitle : shareDesc);
|
|
|
+ new ShareAction(activity).withMedia(video)
|
|
|
+ .setPlatform(share_media)
|
|
|
+ .setCallback(shareListener)
|
|
|
+ .share();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (TextUtils.equals(type, LINK_TAG)) {
|
|
|
+ String url = content.optString("url");
|
|
|
+ if (TextUtils.isEmpty(url)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ String thumb = content.optString("thumb");
|
|
|
+ ShareIntentBean bean = new ShareIntentBean();
|
|
|
+ bean.setLinkUrl(url);
|
|
|
+ bean.setTitle(shareTitle);
|
|
|
+ bean.setDes(shareDesc);
|
|
|
+ bean.setShare_media(share_media);
|
|
|
+ bean.setThumb(thumb);
|
|
|
+ toShareUrl(activity, bean, shareListener);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void toShareUrl(Activity activity, ShareIntentBean intentBean, UMShareListener shareListener) {
|
|
|
+ if (intentBean == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ UMWeb web = new UMWeb(intentBean.getLinkUrl());
|
|
|
+ UMImage image;
|
|
|
+ if (!TextUtils.isEmpty(intentBean.getThumb())) {
|
|
|
+ image = new UMImage(activity, intentBean.getThumb());
|
|
|
+ } else {
|
|
|
+ image = new UMImage(activity, AppUtils.getAppIconId());
|
|
|
+ }
|
|
|
+ web.setTitle(intentBean.getTitle());//标题
|
|
|
+ web.setThumb(image);//缩略图
|
|
|
+ web.setDescription(intentBean.getDes());//描述
|
|
|
+ new ShareAction(activity).withMedia(web)
|
|
|
+ .setPlatform(intentBean.getShare_media())
|
|
|
+ .setCallback(shareListener)
|
|
|
+ .share();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public interface ResultCallBack {
|
|
|
+ void onResult(boolean isSuccess);
|
|
|
+ }
|
|
|
+}
|