我想这样做:如何处理在地图中的异常()可观察的RxJava
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中的一个设计缺陷,因为抛出的确切错误无法在映射lambda中的'onError'中传播,而不会被包装为'RuntimeException'。 –