Commit 06131c12775df61d0870a2c12ca8488098e3ff18

Authored by 韩亚云
1 parent 78d1c74d

处理添加本地图片预览出错情况

app/strike/src/main/java/com/cnlive/strike/imgutil/ImgActivity.java
@@ -197,7 +197,6 @@ public class ImgActivity extends AppCompatActivity { @@ -197,7 +197,6 @@ public class ImgActivity extends AppCompatActivity {
197 197
198 @Override 198 @Override
199 public void onDownLoadSuccess(Bitmap bitmap) { 199 public void onDownLoadSuccess(Bitmap bitmap) {
200 - Log.e("sssssss", "onDownLoadSuccess: " );  
201 //网络请求 200 //网络请求
202 bitmaps = bitmap; 201 bitmaps = bitmap;
203 Message msg = new Message(); 202 Message msg = new Message();
core/imageload/src/main/java/com/cnlive/core/imageload/preview/presenter/PreviewImgPresenter.java
@@ -39,62 +39,68 @@ public class PreviewImgPresenter extends BasePresenter<PreviewImgView> { @@ -39,62 +39,68 @@ public class PreviewImgPresenter extends BasePresenter<PreviewImgView> {
39 39
40 public void showImg(Context context, ImagePreviewShowInfo previewData) { 40 public void showImg(Context context, ImagePreviewShowInfo previewData) {
41 41
42 - Observable.create(new ObservableOnSubscribe<File>() {  
43 - @Override  
44 - public void subscribe(ObservableEmitter<File> emitter) {  
45 - FutureTarget<File> file = Glide.with(context).asFile().load(previewData.getImgPath()).listener(new RequestListener<File>() {  
46 - @Override  
47 - public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<File> target, boolean isFirstResource) { 42 + if(FileUtils.isFileExists(new File(previewData.getImgPath()))){
  43 + ifViewAttached(view -> view.showResourceReady());
  44 + ifViewAttached(view -> view.showImg(new File(previewData.getImgPath())));
  45 + }else {
  46 + Observable.create(new ObservableOnSubscribe<File>() {
  47 + @Override
  48 + public void subscribe(ObservableEmitter<File> emitter) {
  49 + FutureTarget<File> file = Glide.with(context).asFile().load(previewData.getImgPath()).listener(new RequestListener<File>() {
  50 + @Override
  51 + public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<File> target, boolean isFirstResource) {
48 // ifViewAttached(view -> view.showLoadFail()); 52 // ifViewAttached(view -> view.showLoadFail());
49 - return false;  
50 - } 53 + return false;
  54 + }
51 55
52 - @Override  
53 - public boolean onResourceReady(File resource, Object model, Target<File> target, DataSource dataSource, boolean isFirstResource) { 56 + @Override
  57 + public boolean onResourceReady(File resource, Object model, Target<File> target, DataSource dataSource, boolean isFirstResource) {
54 // ifViewAttached(view -> view.showResourceReady()); 58 // ifViewAttached(view -> view.showResourceReady());
55 - return false;  
56 - }  
57 - }).submit();  
58 - try {  
59 - File imgfile = file.get();  
60 - if (FileUtils.isFileExists(imgfile)) {  
61 - emitter.onNext(imgfile);  
62 - } else { 59 + return false;
  60 + }
  61 + }).submit();
  62 + try {
  63 + File imgfile = file.get();
  64 + if (FileUtils.isFileExists(imgfile)) {
  65 + emitter.onNext(imgfile);
  66 + } else {
  67 + emitter.onError(new Throwable("加载失败"));
  68 + }
  69 + } catch (Exception e) {
63 emitter.onError(new Throwable("加载失败")); 70 emitter.onError(new Throwable("加载失败"));
64 } 71 }
65 - } catch (Exception e) {  
66 - emitter.onError(new Throwable("加载失败")); 72 +
67 } 73 }
  74 + }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<File>() {
68 75
69 - }  
70 - }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<File>() {  
71 76
  77 + @Override
  78 + public void onSubscribe(Disposable d) {
72 79
73 - @Override  
74 - public void onSubscribe(Disposable d) { 80 + }
75 81
76 - } 82 + @Override
  83 + public void onNext(File filse) {
77 84
78 - @Override  
79 - public void onNext(File filse) { 85 + ifViewAttached(view -> view.showResourceReady());
  86 + ifViewAttached(view -> view.showImg(filse));
  87 + }
80 88
81 - ifViewAttached(view -> view.showResourceReady());  
82 - ifViewAttached(view -> view.showImg(filse));  
83 - } 89 + @Override
  90 + public void onError(Throwable e) {
  91 + ifViewAttached(view -> view.showLoadFail());
  92 + if (e != null) {
  93 + ifViewAttached(view -> view.showMessage(e.getMessage()));
  94 + }
84 95
85 - @Override  
86 - public void onError(Throwable e) {  
87 - ifViewAttached(view -> view.showLoadFail());  
88 - if (e != null) {  
89 - ifViewAttached(view -> view.showMessage(e.getMessage()));  
90 } 96 }
91 97
92 - } 98 + @Override
  99 + public void onComplete() {
93 100
94 - @Override  
95 - public void onComplete() { 101 + }
  102 + });
  103 + }
96 104
97 - }  
98 - });  
99 } 105 }
100 } 106 }
core/imageload/src/main/java/com/cnlive/core/imageload/preview/presenter/ProcessHtmlPresenter.java
@@ -93,7 +93,7 @@ public class ProcessHtmlPresenter extends BasePresenter&lt;ProcessHtmlView&gt; { @@ -93,7 +93,7 @@ public class ProcessHtmlPresenter extends BasePresenter&lt;ProcessHtmlView&gt; {
93 public void collectionPic(DataProcessingActivity context, String imagePath) { 93 public void collectionPic(DataProcessingActivity context, String imagePath) {
94 if (context == null && TextUtils.isEmpty(imagePath)) return; 94 if (context == null && TextUtils.isEmpty(imagePath)) return;
95 if (FileUtils.isFileExists(imagePath)) { 95 if (FileUtils.isFileExists(imagePath)) {
96 - if (getView() != null) processLocalImag(context, imagePath); 96 + ifViewAttached(view -> processLocalImag(context, imagePath));
97 } else { 97 } else {
98 if (context.previewData != null) collection(context, imagePath, context.previewData); 98 if (context.previewData != null) collection(context, imagePath, context.previewData);
99 } 99 }
@@ -161,17 +161,17 @@ public class ProcessHtmlPresenter extends BasePresenter&lt;ProcessHtmlView&gt; { @@ -161,17 +161,17 @@ public class ProcessHtmlPresenter extends BasePresenter&lt;ProcessHtmlView&gt; {
161 * @param url 161 * @param url
162 */ 162 */
163 public void savePicture(Context context, String url) { 163 public void savePicture(Context context, String url) {
164 - if (getView() != null) getView().hideLoading(); 164 + ifViewAttached(view -> view.hideLoading());
165 if (TextUtils.isEmpty(url)) { 165 if (TextUtils.isEmpty(url)) {
166 - if (getView() != null) getView().showMessage("非法图片地址"); 166 + ifViewAttached(view -> view.showMessage("非法图片地址"));
167 return; 167 return;
168 } 168 }
169 if (FileUtils.isFileExists(url)) { 169 if (FileUtils.isFileExists(url)) {
170 // 本地图片 做copy操作 170 // 本地图片 做copy操作
171 - if (getView() != null) getView().saveLocalImg(url); 171 + ifViewAttached(view -> view.saveLocalImg(url));
172 } else { 172 } else {
173 if (!url.contains("http")) { 173 if (!url.contains("http")) {
174 - if (getView() != null) getView().showMessage("非法图片地址"); 174 + ifViewAttached(view -> view.showMessage("非法图片地址"));
175 return; 175 return;
176 } 176 }
177 Observable.create(new ObservableOnSubscribe<String>() { 177 Observable.create(new ObservableOnSubscribe<String>() {
@@ -197,16 +197,16 @@ public class ProcessHtmlPresenter extends BasePresenter&lt;ProcessHtmlView&gt; { @@ -197,16 +197,16 @@ public class ProcessHtmlPresenter extends BasePresenter&lt;ProcessHtmlView&gt; {
197 197
198 @Override 198 @Override
199 public void onNext(String s) { 199 public void onNext(String s) {
200 - if (!TextUtils.isEmpty(s) && getView() != null) {  
201 - getView().saveLocalImg(s); 200 + if (!TextUtils.isEmpty(s)) {
  201 + ifViewAttached(view -> view.saveLocalImg(s));
202 } else { 202 } else {
203 - if (getView() != null) getView().showMessage("数据异常"); 203 + ifViewAttached(view -> view.showMessage("数据异常"));
204 } 204 }
205 } 205 }
206 206
207 @Override 207 @Override
208 public void onError(Throwable e) { 208 public void onError(Throwable e) {
209 - if (getView() != null) getView().showMessage(e.getMessage()); 209 + ifViewAttached(view -> view.showMessage(e.getMessage()));
210 } 210 }
211 211
212 @Override 212 @Override
@@ -247,24 +247,26 @@ public class ProcessHtmlPresenter extends BasePresenter&lt;ProcessHtmlView&gt; { @@ -247,24 +247,26 @@ public class ProcessHtmlPresenter extends BasePresenter&lt;ProcessHtmlView&gt; {
247 247
248 @Override 248 @Override
249 public void onNext(String content) { 249 public void onNext(String content) {
250 - if (getView() != null) getView().hideLoading(); 250 + ifViewAttached(view -> view.hideLoading());
251 if (!TextUtils.isEmpty(content)) { 251 if (!TextUtils.isEmpty(content)) {
252 if (type.equals("friends")) { 252 if (type.equals("friends")) {
253 - if (getView() != null) getView().forwardFriends(content); 253 + ifViewAttached(view -> view.forwardFriends(content));
254 } else if (type.equals("life")) { 254 } else if (type.equals("life")) {
255 - if (getView() != null) getView().forwardLife(content); 255 + ifViewAttached(view -> view.forwardLife(content));
256 } else { 256 } else {
257 - if (getView() != null) getView().showMessage("类型异常"); 257 + ifViewAttached(view -> view.showMessage("类型异常"));
258 } 258 }
259 } else { 259 } else {
260 - if (getView() != null) getView().showMessage("非法图片地址"); 260 + ifViewAttached(view -> view.showMessage("非法图片地址"));
261 } 261 }
262 } 262 }
263 263
264 @Override 264 @Override
265 public void onError(Throwable e) { 265 public void onError(Throwable e) {
266 - if (getView() != null) getView().hideLoading();  
267 - if (getView() != null) getView().showMessage(e.getMessage()); 266 + ifViewAttached(view -> {
  267 + view.hideLoading();
  268 + view.showMessage(e.getMessage());
  269 + });
268 } 270 }
269 271
270 @Override 272 @Override
@@ -278,11 +280,11 @@ public class ProcessHtmlPresenter extends BasePresenter&lt;ProcessHtmlView&gt; { @@ -278,11 +280,11 @@ public class ProcessHtmlPresenter extends BasePresenter&lt;ProcessHtmlView&gt; {
278 280
279 public void saveVideo(Activity activity, String imgUrl) { 281 public void saveVideo(Activity activity, String imgUrl) {
280 if (TextUtils.isEmpty(imgUrl)) { 282 if (TextUtils.isEmpty(imgUrl)) {
281 - if (getView() != null) getView().showMessage("数据异常"); 283 + ifViewAttached(view -> view.showMessage("数据异常"));
282 return; 284 return;
283 } 285 }
284 if (imgUrl.contains("http")) { 286 if (imgUrl.contains("http")) {
285 - if (getView() != null) getView().saveNetVideo(imgUrl); 287 + ifViewAttached(view -> view.saveNetVideo(imgUrl));
286 } else { 288 } else {
287 saveVideo(activity, FileUtils.getFileByPath(imgUrl)); 289 saveVideo(activity, FileUtils.getFileByPath(imgUrl));
288 } 290 }
@@ -321,7 +323,7 @@ public class ProcessHtmlPresenter extends BasePresenter&lt;ProcessHtmlView&gt; { @@ -321,7 +323,7 @@ public class ProcessHtmlPresenter extends BasePresenter&lt;ProcessHtmlView&gt; {
321 outputStream.close(); 323 outputStream.close();
322 324
323 context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + newFile.getAbsolutePath()))); 325 context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + newFile.getAbsolutePath())));
324 - getView().showSucessfulMessage("保存成功"); 326 + ifViewAttached(view -> view.showSucessfulMessage("保存成功"));
325 context.finish(); 327 context.finish();
326 } catch (Exception e) { 328 } catch (Exception e) {
327 Log.e("文件保存异常:", "Exception ", e); 329 Log.e("文件保存异常:", "Exception ", e);
@@ -377,16 +379,16 @@ public class ProcessHtmlPresenter extends BasePresenter&lt;ProcessHtmlView&gt; { @@ -377,16 +379,16 @@ public class ProcessHtmlPresenter extends BasePresenter&lt;ProcessHtmlView&gt; {
377 public void onNext(String qrContent) { 379 public void onNext(String qrContent) {
378 // if (getView()!=null)getView().hideLoading(); 380 // if (getView()!=null)getView().hideLoading();
379 if (qrContent != null && qrContent.length() > 0) { 381 if (qrContent != null && qrContent.length() > 0) {
380 - if (getView() != null) getView().processQrContent(qrContent); 382 + ifViewAttached(view -> view.processQrContent(qrContent));
381 } else { 383 } else {
382 - getView().showMessage("未识别二维码"); 384 + ifViewAttached(view -> view.showMessage("未识别二维码"));
383 } 385 }
384 } 386 }
385 387
386 @Override 388 @Override
387 public void onError(Throwable e) { 389 public void onError(Throwable e) {
388 - if (getView() != null) getView().hideLoading();  
389 - if (getView() != null) getView().showMessage(e.getMessage()); 390 + ifViewAttached(view -> view.hideLoading());
  391 + ifViewAttached(view -> view.showMessage(e.getMessage()));
390 } 392 }
391 393
392 @Override 394 @Override