Commit e2534caccf4ce49f7b8c320b4859ef5f7a5bb71c

Authored by 韩亚云
1 parent aa8ba763

图片预览长按保存事件优化

app/strike/src/main/java/com/cnlive/strike/imgutil/ImgActivity.java
... ... @@ -94,6 +94,7 @@ public class ImgActivity extends AppCompatActivity {
94 94 findViewById(R.id.pre_btn).setOnClickListener(view -> {
95 95 PreViewImgData data = new PreViewImgData();
96 96 data.setType("1");
  97 + data.setUserId("10514343");
97 98 PreViewImgInfo cuData = new PreViewImgInfo();
98 99 cuData.setImgUrl("https://b-ssl.duitang.com/uploads/item/201505/09/20150509221719_kyNrM.jpeg");
99 100 data.setCurrPrewInfo(cuData);
... ... @@ -107,6 +108,10 @@ public class ImgActivity extends AppCompatActivity {
107 108 cuData2.setImgUrl("https://b-ssl.duitang.com/uploads/item/201505/11/20150511122951_MAwVZ.jpeg");
108 109 preViewImgInfos.add(cuData2);
109 110  
  111 + PreViewImgInfo preViewImgInfo = new PreViewImgInfo();
  112 + preViewImgInfo.setImgUrl("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1583074722432&di=7ba0e34112d2c6e3b870fa7ce18827b1&imgtype=0&src=http%3A%2F%2Fm.dazhuanglawyer.com%2Ftupian%2F20190904%2F201909041739423115.jpg");
  113 + preViewImgInfos.add(preViewImgInfo);
  114 +
110 115 data.setPicInfoList(preViewImgInfos);
111 116 data.setFriend(true);
112 117 data.setCollection(true);
... ... @@ -224,7 +229,7 @@ public class ImgActivity extends AppCompatActivity {
224 229  
225 230 // .asSquare()//正方形
226 231 findViewById(R.id.down).setOnClickListener(view -> {
227   - ImageLoader.saveImageIntoGallery(new DownLoadImageService(ImgActivity.this, URL1, true, "lala", new ImageDownLoadCallBack() {
  232 + ImageLoader.saveImageIntoGallery(new DownLoadImageService(ImgActivity.this, URL1, false, "lala", new ImageDownLoadCallBack() {
228 233  
229 234 @Override
230 235 public void onDownLoadSuccess(Bitmap bitmap) {
... ...
cloud/user/src/main/java/com/cnlive/cloud/user/preview/ui/widget/DownLoadImageService.java
... ... @@ -78,7 +78,7 @@ public class DownLoadImageService implements Runnable {
78 78 inStream.close();
79 79 outputStream.close();
80 80  
81   - context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + newFile.getAbsolutePath())));
  81 + context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse(newFile.getAbsolutePath())));
82 82 //TODO 文件缓存已经完成 待优化~!!
83 83 } catch (Exception e) {
84 84 Log.e("ImageDownloadService", "Exception ", e);
... ...
core/app_qr/src/main/java/com/cnlive/cloud/moudle/qr/frame/view/GenerateQRView.java
... ... @@ -189,7 +189,7 @@ public class GenerateQRView extends BaseView {
189 189 // e.printStackTrace();
190 190 // }
191 191 // 最后通知图库更新
192   - context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + file.getAbsolutePath())));
  192 + context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse(file.getAbsolutePath())));
193 193  
194 194 AlertUtil.showSuccessToast(mContext, "保存成功");
195 195  
... ...
core/imageload/src/main/java/com/cnlive/core/imageload/preview/presenter/ProcessHtmlPresenter.java
... ... @@ -321,7 +321,7 @@ public class ProcessHtmlPresenter extends BasePresenter<ProcessHtmlView> {
321 321 inStream.close();
322 322 outputStream.close();
323 323  
324   - context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + newFile.getAbsolutePath())));
  324 + context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse(newFile.getAbsolutePath())));
325 325 ifViewAttached(view -> view.showSucessfulMessage("保存成功"));
326 326 context.finish();
327 327 } catch (Exception e) {
... ...
core/imageload/src/main/java/com/cnlive/core/imageload/preview/view/ProcessHtmlView.java
... ... @@ -149,7 +149,7 @@ public class ProcessHtmlView extends BaseView {
149 149 if (localContentValues != null)
150 150 localContentResolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, localContentValues);
151 151 }
152   - mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + s)));
  152 + mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse(s)));
153 153 }
154 154 } catch (Exception e) {
155 155 e.printStackTrace();
... ... @@ -273,7 +273,7 @@ public class ProcessHtmlView extends BaseView {
273 273 file.getAbsolutePath(), newFile.getName(), null);
274 274  
275 275 }
276   - context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + newFile.getAbsolutePath())));
  276 + context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse(newFile.getAbsolutePath())));
277 277 showSucessfulMessage("保存成功");
278 278 } catch (Exception e) {
279 279 if (e != null && e.getMessage() != null)
... ...
core/imageload/src/main/java/com/cnlive/core/imageload/util/DownLoadImageService.java
... ... @@ -30,6 +30,7 @@ public class DownLoadImageService implements Runnable{
30 30 private File currentFile;
31 31 private String fileName;
32 32  
  33 + //是否加入系统图库
33 34 private boolean isSetMediaStore;
34 35  
35 36 public DownLoadImageService(Context context, String url,boolean isSetMediaStore , String fileName, ImageDownLoadCallBack callBack) {
... ... @@ -67,9 +68,7 @@ public class DownLoadImageService implements Runnable{
67 68  
68 69 public void saveImageToGallery(Context context, Bitmap bmp) {
69 70 // 首先保存图片
70   -
71 71 File file = Build.VERSION.SDK_INT > 28?context.getExternalFilesDir(Environment.DIRECTORY_PICTURES).getAbsoluteFile():Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsoluteFile();//注意小米手机必须这样获得public绝对路径
72   -
73 72 File appDir = new File(file ,fileName);
74 73 if (!appDir.exists()) {
75 74 appDir.mkdirs();
... ... @@ -99,7 +98,6 @@ public class DownLoadImageService implements Runnable{
99 98 if(isSetMediaStore){
100 99 setMediaDtore(fileName);
101 100 }
102   -
103 101 // 最后通知图库更新
104 102 context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
105 103 Uri.fromFile(new File(currentFile.getPath()))));
... ...
core/imageload/src/main/java/com/cnlive/core/ui/presenter/PreViewImagePresenter.java
... ... @@ -170,10 +170,9 @@ public class PreViewImagePresenter extends BasePresenter<PreViewImageView> {
170 170  
171 171 private void collection(PreViewImageActivity context, String imagePath, PreViewImgData previewData) {
172 172 if (previewData == null) return;
173   - String sid = UserService.getUid(context);
174 173 Map<String, String> requestMap = new HashMap<>();
175 174 requestMap.put("appId", AppConfig.getAppId());
176   - requestMap.put("sid", sid);
  175 + requestMap.put("sid", previewData.getUserId());
177 176 requestMap.put("type", previewData.getType());
178 177 requestMap.put("img", imagePath);
179 178  
... ... @@ -213,86 +212,18 @@ public class PreViewImagePresenter extends BasePresenter&lt;PreViewImageView&gt; {
213 212  
214 213 @Override
215 214 public void onSuccess(BaseResult data) {
216   - ifViewAttached(view -> view.showSucessfulMessage("收藏成功"));
  215 + if(getView() != null) getView().showSucessfulMessage("收藏成功");
  216 +// ifViewAttached(view -> view.showSucessfulMessage("收藏成功"));
217 217 }
218 218  
219 219 @Override
220 220 public void onFailure(String errCode, String errMsg, Exception e) {
221   - ifViewAttached(view -> view.showMessage(errMsg));
  221 + if(getView() != null) getView().showMessage(errMsg);
  222 +// ifViewAttached(view -> view.showMessage(errMsg));
222 223 }
223 224 });
224 225 }
225 226  
226   -
227   - /**
228   - * 保存图片
229   - *
230   - * @param url
231   - */
232   - public void savePicture(Activity context, String url) {
233   - if(getView() != null) getView().hideLoading();
234   -// ifViewAttached(view -> view.hideLoading());
235   - if (TextUtils.isEmpty(url)) {
236   - if(getView() != null) getView().showMessage("非法图片地址");
237   -// ifViewAttached(view -> view.showMessage("非法图片地址"));
238   - return;
239   - }
240   - if (FileUtils.isFileExists(url)) {
241   -// 本地图片 做copy操作
242   - if(getView() != null) getView().saveLocalImg(url);
243   -// ifViewAttached(view -> view.saveLocalImg(url));
244   - } else {
245   - if (!url.contains("http")) {
246   - if(getView() != null) getView().showMessage("非法图片地址");
247   -// ifViewAttached(view -> view.showMessage("非法图片地址"));
248   - return;
249   - }
250   - Observable.create(new ObservableOnSubscribe<String>() {
251   - @Override
252   - public void subscribe(ObservableEmitter<String> emitter) throws Exception {
253   - if (url.contains("http")) {
254   - File file = Glide.with(context).asFile().load(url).submit().get();
255   - if (file != null && file.exists() && file.length() > 0) {
256   - String path = file.getAbsolutePath();
257   - emitter.onNext(path);
258   - } else {
259   - emitter.onError(new Throwable("非法图片地址"));
260   - }
261   - } else {
262   - emitter.onError(new Throwable("非法图片地址"));
263   - }
264   - }
265   - }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<String>() {
266   - @Override
267   - public void onSubscribe(Disposable d) {
268   -
269   - }
270   -
271   - @Override
272   - public void onNext(String s) {
273   - if (!TextUtils.isEmpty(s)) {
274   - if(getView() != null) getView().saveLocalImg(s);
275   -// ifViewAttached(view -> view.saveLocalImg(s));
276   - } else {
277   - if(getView() != null) getView().showMessage("数据异常");
278   -// ifViewAttached(view -> view.showMessage("数据异常"));
279   - }
280   - }
281   -
282   - @Override
283   - public void onError(Throwable e) {
284   - if(getView() != null) getView().showMessage(e.getMessage());
285   -// ifViewAttached(view -> view.showMessage(e.getMessage()));
286   - }
287   -
288   - @Override
289   - public void onComplete() {
290   -
291   - }
292   - });
293   - }
294   - }
295   -
296 227 /**
297 228 * 保存视频
298 229 *
... ... @@ -332,7 +263,7 @@ public class PreViewImagePresenter extends BasePresenter&lt;PreViewImageView&gt; {
332 263 outputStream.write(buffer2, 0, byteread);
333 264 inStream.close();
334 265 outputStream.close();
335   - activity.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + newFile.getAbsolutePath())));
  266 + activity.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse(newFile.getAbsolutePath())));
336 267 ifViewAttached(view -> view.showSucessfulMessage("保存成功"));
337 268 activity.finish();
338 269 } catch (Exception e) {
... ... @@ -348,12 +279,11 @@ public class PreViewImagePresenter extends BasePresenter&lt;PreViewImageView&gt; {
348 279 * @param imgUrl
349 280 */
350 281 public void isScan(Activity activity,String imgUrl,int position) {
351   - if (TextUtils.isEmpty(imgUrl)) {
352   - return;
353   - }
354   - if (!NetworkUtil.isConnectInternet(activity)) {
  282 + if (TextUtils.isEmpty(imgUrl) || !NetworkUtil.isConnectInternet(activity)) {
  283 + if(getView() != null) getView().showDialog(position,false);
355 284 return;
356 285 }
  286 +
357 287 Observable.create(new ObservableOnSubscribe<String>() {
358 288 @Override
359 289 public void subscribe(ObservableEmitter<String> emitter) throws Exception {
... ... @@ -483,14 +413,19 @@ public class PreViewImagePresenter extends BasePresenter&lt;PreViewImageView&gt; {
483 413 if (getView()!=null)getView().processQrContent(qrContent);
484 414 // ifViewAttached(view -> view.processQrContent(qrContent));
485 415 } else {
486   - ifViewAttached(view -> view.showMessage("未识别二维码"));
  416 + if(getView() != null) getView().showMessage("未识别二维码");
  417 +// ifViewAttached(view -> view.showMessage("未识别二维码"));
487 418 }
488 419 }
489 420  
490 421 @Override
491 422 public void onError(Throwable e) {
492   - ifViewAttached(view -> view.hideLoading());
493   - ifViewAttached(view -> view.showMessage(e.getMessage()));
  423 + if(getView() != null){
  424 + getView().hideLoading();
  425 + getView().showMessage(e.getMessage());
  426 + }
  427 +// ifViewAttached(view -> view.hideLoading());
  428 +// ifViewAttached(view -> view.showMessage(e.getMessage()));
494 429 }
495 430  
496 431 @Override
... ...
core/imageload/src/main/java/com/cnlive/core/ui/view/PreViewImageView.java
... ... @@ -6,11 +6,12 @@ import android.app.Dialog;
6 6 import android.content.ContentResolver;
7 7 import android.content.ContentValues;
8 8 import android.content.Intent;
  9 +import android.graphics.Bitmap;
9 10 import android.net.Uri;
10   -import android.os.Environment;
  11 +import android.os.Handler;
  12 +import android.os.Message;
11 13 import android.provider.MediaStore;
12 14 import android.text.TextUtils;
13   -import android.util.Log;
14 15 import android.view.Gravity;
15 16 import android.view.View;
16 17 import android.view.ViewGroup;
... ... @@ -20,16 +21,17 @@ import android.widget.ImageView;
20 21 import com.cnlive.cloud.moudle.qr.uitl.IdentifyTheQRCodeCallback;
21 22 import com.cnlive.cloud.moudle.qr.uitl.QrContentUtil;
22 23 import com.cnlive.core.imageload.R;
  24 +import com.cnlive.core.imageload.callback.ImageDownLoadCallBack;
  25 +import com.cnlive.core.imageload.loader.ImageLoader;
23 26 import com.cnlive.core.imageload.preview.router.PreviewRouters;
24 27 import com.cnlive.core.imageload.preview.widget.CustomTipDialog;
25 28 import com.cnlive.core.imageload.preview.widget.DownloadUtil;
  29 +import com.cnlive.core.imageload.util.DownLoadImageService;
26 30 import com.cnlive.core.imagepreview.listener.OnItemLongPressListener;
27 31 import com.cnlive.core.libs.base.frame.view.BaseView;
28 32 import com.cnlive.core.libs.base.interfaces.OnPermissionResponseListener;
29 33 import com.cnlive.core.libs.base.util.AlertUtil;
30   -import com.cnlive.core.libs.base.util.FileTypeUtil;
31 34 import com.cnlive.core.libs.base.util.FileUtils;
32   -import com.cnlive.core.libs.base.util.MD5Util;
33 35 import com.cnlive.core.ui.activity.PreViewImageActivity;
34 36 import com.cnlive.core.ui.model.PreViewImgData;
35 37 import com.cnlive.core.ui.model.PreViewImgInfo;
... ... @@ -37,12 +39,10 @@ import com.cnlive.media.picker.entity.Media;
37 39 import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
38 40  
39 41 import java.io.File;
40   -import java.io.FileInputStream;
41   -import java.io.FileOutputStream;
42   -import java.io.InputStream;
43 42 import java.util.ArrayList;
44 43 import java.util.List;
45 44  
  45 +import androidx.annotation.NonNull;
46 46 import io.reactivex.Observable;
47 47 import io.reactivex.ObservableEmitter;
48 48 import io.reactivex.ObservableOnSubscribe;
... ... @@ -61,6 +61,17 @@ public class PreViewImageView extends BaseView {
61 61 private List<PreViewImgInfo> preViewImgList = new ArrayList<>();
62 62 private Dialog dialog;
63 63 private CustomTipDialog loadingDiaog;
  64 + @SuppressLint("HandlerLeak")
  65 + private Handler handler = new Handler(){
  66 + @Override
  67 + public void handleMessage(@NonNull Message msg) {
  68 + switch (msg.what){
  69 + case 0:
  70 + showSucessfulMessage(msg.obj.toString());
  71 + break;
  72 + }
  73 + }
  74 + };
64 75  
65 76 public PreViewImageView(PreViewImageActivity activity) {
66 77 this.activity = activity;
... ... @@ -74,9 +85,7 @@ public class PreViewImageView extends BaseView {
74 85 showError();
75 86 }
76 87  
77   - PreViewImgInfo preViewImgInfo = new PreViewImgInfo();
78   - preViewImgInfo.setImgUrl("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1583074722432&di=7ba0e34112d2c6e3b870fa7ce18827b1&imgtype=0&src=http%3A%2F%2Fm.dazhuanglawyer.com%2Ftupian%2F20190904%2F201909041739423115.jpg");
79   - activity.data.getPicInfoList().add(preViewImgInfo);
  88 +
80 89 if (activity.type.equals("1")) {
81 90 preViewImgList = activity.data.getPicInfoList();
82 91 if (preViewImgList.size() == 1) {
... ... @@ -103,7 +112,6 @@ public class PreViewImageView extends BaseView {
103 112 .imageData(activity.picList)
104 113 .playExitAnim(false);
105 114 activity.binding.imagePre.watch(activity.position);
106   -
107 115 activity.binding.imagePre.setOnItemLongPressListener(new OnItemLongPressListener() {
108 116 @Override
109 117 public boolean onItemLongPress(int position, ImageView imageView) {
... ... @@ -111,7 +119,6 @@ public class PreViewImageView extends BaseView {
111 119 @Override
112 120 public void onSuccess(String[] permissions) {
113 121 activity.getPresenter().isScan(activity,activity.picList.get(position),position);
114   -
115 122 }
116 123  
117 124 @Override
... ... @@ -147,7 +154,6 @@ public class PreViewImageView extends BaseView {
147 154 dialog.findViewById(R.id.share_friend).setOnClickListener(view -> {
148 155 dialog.dismiss();
149 156 activity.getPresenter().shareFriendLife(activity, "friends", TextUtils.isEmpty(preViewImgList.get(position).getImgUrl()) ? preViewImgList.get(position).getImgUrl() : preViewImgList.get(position).getImgUrl());
150   -
151 157 });
152 158 dialog.findViewById(R.id.share_cirfriend).setOnClickListener(view -> {
153 159 dialog.dismiss();
... ... @@ -160,7 +166,7 @@ public class PreViewImageView extends BaseView {
160 166 dialog.findViewById(R.id.save_picture).setOnClickListener(view -> {
161 167 dialog.dismiss();
162 168 if (activity.data.getType().equals("1")) {
163   - activity.getPresenter().savePicture(activity, TextUtils.isEmpty(preViewImgList.get(position).getImgUrl()) ? preViewImgList.get(position).getImgUrl() : preViewImgList.get(position).getImgUrl());
  169 + savePicture(TextUtils.isEmpty(preViewImgList.get(position).getImgUrl()) ? preViewImgList.get(position).getImgUrl() : preViewImgList.get(position).getImgUrl());
164 170 } else if (activity.data.getType().equals("1")) {
165 171 activity.getPresenter().saveVideo(activity, TextUtils.isEmpty(preViewImgList.get(position).getImgUrl()) ? preViewImgList.get(position).getImgUrl() : preViewImgList.get(position).getImgUrl());
166 172 }
... ... @@ -211,46 +217,27 @@ public class PreViewImageView extends BaseView {
211 217 }
212 218  
213 219 /**
214   - * 复制本地图片到指定文件夹下
215   - *
216   - * @param imagePath
  220 + * 保存图片
  221 + * @param url
217 222 */
218   - public void saveLocalImg(String imagePath) {
219   - File file = new File(imagePath);
220   - try {
221   - String md5 = MD5Util.getMD5(file);
222   - InputStream inStream = new FileInputStream(file); //读入原文件
223   - File appDir = new File(Environment.getExternalStorageDirectory(), "strike");
224   - if (!appDir.exists()) appDir.mkdir();
225   -
226   - String type = FileTypeUtil.getFileType(file.getAbsolutePath(), true);
227   - String extension;//初始文件扩展名
228   - if (TextUtils.isEmpty(type)) {
229   - extension = ".jpg";
230   - } else {
231   - extension = "." + type;
  223 + public void savePicture(String url) {
  224 + ImageLoader.saveImageIntoGallery(new DownLoadImageService(activity, url, false, "strike", new ImageDownLoadCallBack() {
  225 + @Override
  226 + public void onDownLoadSuccess(Bitmap bitmap) {
  227 + Message message = new Message();
  228 + message.what = 0;
  229 + message.obj = "保存成功";
  230 + handler.sendMessage(message);
232 231 }
233 232  
234   - File newFile = new File(appDir, md5 + extension);
235   - FileOutputStream outputStream = new FileOutputStream(newFile);
236   -
237   - int byteread = 0;
238   - byte[] buffer2 = new byte[1444];
239   - while ((byteread = inStream.read(buffer2)) != -1)
240   - outputStream.write(buffer2, 0, byteread);
241   - inStream.close();
242   - outputStream.close();
243   - if (newFile != null) {
244   - MediaStore.Images.Media.insertImage(activity.getContentResolver(),
245   - file.getAbsolutePath(), newFile.getName(), null);
246   -
  233 + @Override
  234 + public void onDownLoadFailed() {
  235 + Message message = new Message();
  236 + message.what = 0;
  237 + message.obj = "保存出错";
  238 + handler.sendMessage(message);
247 239 }
248   - activity.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + newFile.getAbsolutePath())));
249   - showSucessfulMessage("保存成功");
250   - } catch (Exception e) {
251   - if (e != null && e.getMessage() != null)
252   - Log.e("保存图片异常", e.getMessage());
253   - }
  240 + }));
254 241 }
255 242  
256 243 /**
... ... @@ -312,7 +299,7 @@ public class PreViewImageView extends BaseView {
312 299 if (localContentValues != null)
313 300 localContentResolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, localContentValues);
314 301 }
315   - activity.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + s)));
  302 + activity.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse(s)));
316 303 }
317 304 } catch (Exception e) {
318 305 e.printStackTrace();
... ... @@ -362,8 +349,9 @@ public class PreViewImageView extends BaseView {
362 349 hideLoading();
363 350 if (!isIdentify) {
364 351 showMessage(message);
  352 + }else {
  353 + if (activity != null) activity.finish();
365 354 }
366   - if (activity != null) activity.finish();
367 355 }
368 356 });
369 357 }
... ...