Created
April 30, 2015 05:24
-
-
Save jayrambhia/0b3f690c3b79a413fa92 to your computer and use it in GitHub Desktop.
Observable image loader.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// LoaderCallback is just some callback that I use | |
private void fetchBitmap(String path, final LoaderCallback loaderCallback){ | |
// Retrofit call. | |
Observable<Response> responseObservable = loaderService.getImage(path); | |
// ObservableBitmap is a class that wraps Bitmap and has other fields like local_path, remote_path. | |
responseObservable.flatMap(new Func1<Response, Observable<ObservableBitmap>>() { | |
@Override | |
public Observable<ObservableBitmap> call(final Response response) { | |
Observable<ObservableBitmap> bitmapObservable = Observable.create(new Observable.OnSubscribe<ObservableBitmap>() { | |
@Override | |
public void call(Subscriber<? super ObservableBitmap> subscriber) { | |
try { | |
// response.getUrl(); | |
Log.i(TAG1, "downloaded URL: " + response.getUrl()); | |
Log.i(TAG1, "Note URL: " + loaderCallback.getURL()); | |
Bitmap bitmap = BitmapFactory.decodeStream(response.getBody().in()); | |
ObservableBitmap observableBitmap = new ObservableBitmap(bitmap); | |
bitmap.recycle(); | |
String local_path = saveBitmap(observableBitmap.getBitmap()); | |
observableBitmap.setLocal_path(local_path); | |
observableBitmap.setRemote_url(response.getUrl()); | |
Log.i(TAG1, "observable bitmap url: " + observableBitmap.getRemote_url()); | |
Log.i(TAG1, "observable bitmap size: " + String.valueOf(observableBitmap.getWidth()) | |
+ " " + String.valueOf(observableBitmap.getHeight())); | |
// observableBitmap.setLocal_path(local_path); | |
subscriber.onNext(observableBitmap); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
subscriber.onNext(null); | |
} | |
} | |
}); | |
return bitmapObservable; | |
} | |
}).subscribeOn(Schedulers.io()) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe(new Action1<ObservableBitmap>() { | |
@Override | |
public void call(ObservableBitmap observableBitmap) { | |
if (observableBitmap != null) { | |
loaderCallback.onSuccess(observableBitmap); | |
} else { | |
loaderCallback.onError(new Exception("null bitmap"), -1); | |
} | |
} | |
}, new Action1<Throwable>() { | |
@Override | |
public void call(Throwable throwable) { | |
throwable.printStackTrace(); | |
if (throwable.getClass().getName().equals(RetrofitError.class.getName())) { | |
RetrofitError error = (RetrofitError)throwable; | |
Log.i(TAG, error.getUrl()); | |
Log.i(TAG, "kind " + error.getKind().toString()); | |
if (error.getSuccessType() != null) { | |
Log.i(TAG, "successType :" + error.getSuccessType().toString()); | |
} else { | |
Log.e(TAG, "successType is null"); | |
} | |
if (error.getResponse() != null) { | |
Log.i(TAG, "error status: " + error.getResponse().getStatus()); | |
Log.i(TAG, "error reason: " + error.getResponse().getReason()); | |
int status = error.getResponse().getStatus(); | |
loaderCallback.onError(throwable, status); | |
return; | |
} else { | |
Log.e(TAG, "response is null"); | |
} | |
// loaderCallback.onError(error); | |
} | |
loaderCallback.onError(throwable, -1); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment