【发布时间】:2016-02-06 23:13:15
【问题描述】:
我想这样做:
Observable.just(bitmap)
.map(new Func1<Bitmap, File>() {
@Override
public File call(Bitmap photoBitmap) {
//File creation throws IOException,
//I just want it to hit the onError() inside subscribe()
File photoFile = new File(App.getAppContext().getCacheDir(), "userprofilepic_temp.jpg");
if(photoFile.isFile()) {//delete the file first if it exists otherwise the new file won't be created
photoFile.delete();
}
photoFile.createNewFile(); //saves the file in the cache dir
FileOutputStream fos = new FileOutputStream(photoFile);
photoBitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos);//jpeg format
fos.close();
return photoFile;
}
})
.subscribe(//continue implementation...);
基本上在call()方法中,它可以抛出异常。如何让观察者在onError() 中处理它。或者这不是正确的思考方式?
【问题讨论】:
-
请注意,在 RxJava 2 中,
map等运算符允许从 lambda 中抛出已检查的异常。这确实是 RxJava 1 中的一个设计缺陷,因为如果不包装为RuntimeException,则无法在映射 lambda 的 'onError` 中传播引发的确切错误。
标签: java android rx-java rx-android