From e495243d94885db780d3e3a337f0fe3395338019 Mon Sep 17 00:00:00 2001 From: miguelbcr Date: Wed, 25 Jan 2017 10:27:16 +0100 Subject: [PATCH] Handle FileNotFoundException --- .../interactors/DownloadImage.java | 23 +++++++++++-------- .../rx_paparazzo2/interactors/PickImage.java | 7 +++++- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/rx_paparazzo/src/main/java/com/miguelbcr/ui/rx_paparazzo2/interactors/DownloadImage.java b/rx_paparazzo/src/main/java/com/miguelbcr/ui/rx_paparazzo2/interactors/DownloadImage.java index 3d803f6..47b41c1 100644 --- a/rx_paparazzo/src/main/java/com/miguelbcr/ui/rx_paparazzo2/interactors/DownloadImage.java +++ b/rx_paparazzo/src/main/java/com/miguelbcr/ui/rx_paparazzo2/interactors/DownloadImage.java @@ -24,6 +24,7 @@ import io.reactivex.schedulers.Schedulers; import java.io.BufferedInputStream; import java.io.File; +import java.io.FileNotFoundException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; @@ -50,16 +51,18 @@ public DownloadImage with(Uri uri) { private Observable getObservableDownloadFile() { return Observable.create(new ObservableOnSubscribe() { @Override public void subscribe(ObservableEmitter subscriber) throws Exception { - try { - if ("content".equalsIgnoreCase(uri.getScheme())) { - subscriber.onNext(getContent()); - } else { - subscriber.onNext(downloadFile()); - } + if (!subscriber.isDisposed()) { + try { + if ("content".equalsIgnoreCase(uri.getScheme())) { + subscriber.onNext(getContent()); + } else { + subscriber.onNext(downloadFile()); + } - subscriber.onComplete(); - } catch (Exception e) { - subscriber.onError(e); + subscriber.onComplete(); + } catch (FileNotFoundException e) { + subscriber.onError(e); + } } } }).subscribeOn(Schedulers.io()); @@ -77,7 +80,7 @@ private String downloadFile() throws Exception { return file.getAbsolutePath(); } - private String getContent() throws Exception { + private String getContent() throws FileNotFoundException { InputStream inputStream = targetUi.getContext().getContentResolver().openInputStream(uri); String filename = getFilename(uri); filename += imageUtils.getFileExtension(uri); diff --git a/rx_paparazzo/src/main/java/com/miguelbcr/ui/rx_paparazzo2/interactors/PickImage.java b/rx_paparazzo/src/main/java/com/miguelbcr/ui/rx_paparazzo2/interactors/PickImage.java index 0f91620..bc8c5ed 100644 --- a/rx_paparazzo/src/main/java/com/miguelbcr/ui/rx_paparazzo2/interactors/PickImage.java +++ b/rx_paparazzo/src/main/java/com/miguelbcr/ui/rx_paparazzo2/interactors/PickImage.java @@ -20,7 +20,11 @@ import android.content.Intent; import android.net.Uri; import android.support.annotation.Nullable; +import android.text.TextUtils; import io.reactivex.Observable; +import io.reactivex.ObservableSource; +import io.reactivex.exceptions.Exceptions; +import io.reactivex.functions.Consumer; import io.reactivex.functions.Function; import io.reactivex.schedulers.Schedulers; import java.io.File; @@ -66,7 +70,8 @@ public Observable response(int responseCode, @Nullable final Intent inte intent.setData(Uri.fromFile(new File(filePath))); return filePath; } - }); + }) + .onErrorReturnItem(""); } else { return Observable.just(""); }