I want to do this:
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()
中处理它呢?或者这不是正确的思考方式?
map
这样的操作符允许从lambda表达式中抛出已检查异常。这在RxJava 1中实际上是一个设计缺陷,因为无法将抛出的确切错误传播到map
lambda中的'onError',除非将其包装为RuntimeException
。 - Dave Moten