Skip to content

Commit

Permalink
Handle FileNotFoundException
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelbcr committed Jan 25, 2017
1 parent 3c7c70d commit e495243
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -50,16 +51,18 @@ public DownloadImage with(Uri uri) {
private Observable<String> getObservableDownloadFile() {
return Observable.create(new ObservableOnSubscribe<String>() {
@Override public void subscribe(ObservableEmitter<String> 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());
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -66,7 +70,8 @@ public Observable<String> response(int responseCode, @Nullable final Intent inte
intent.setData(Uri.fromFile(new File(filePath)));
return filePath;
}
});
})
.onErrorReturnItem("");
} else {
return Observable.just("");
}
Expand Down

0 comments on commit e495243

Please sign in to comment.