我正在尝试使用Intent下载并发送pdf到PDF应用程序以显示文件,如此处answer of JDenais所示 这是下载pdf并通过Intent传递的代码。
public class PdfOpenHelper {
public static void openPdfFromUrl(final String pdfUrl, final Activity activity) {
Observable.fromCallable(new Callable<File>() {
@Override
public File call() throws Exception {
try {
URL url = new URL(pdfUrl);
URLConnection connection = url.openConnection();
connection.connect();
// download the file
InputStream input = new BufferedInputStream(connection.getInputStream());
File dir = new File(activity.getFilesDir(), "/shared_pdf");
dir.mkdir();
File file = new File(dir, "temp.pdf");
OutputStream output = new FileOutputStream(file);
byte data[] = new byte[1024];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
total += count;
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
return file;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<File>() {
@Override
public void onSubscribe(Subscription s) {
}
@Override
public void onNext(File file) {
String authority = activity.getApplicationContext().getPackageName() + ".fileprovider";
Uri uriToFile = FileProvider.getUriForFile(activity, authority, file);
Intent shareIntent = new Intent(Intent.ACTION_VIEW);
shareIntent.setDataAndType(uriToFile, "application/pdf");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
if (shareIntent.resolveActivity(activity.getPackageManager()) != null) {
activity.startActivity(shareIntent);
}
}
@Override
public void onError(Throwable t) {
}
@Override
public void onComplete() {
}
});
}
}
但我收到了错误
'无法解决
上的方法subscribe(anonymous org.reactivestreams.Subscriber<java.io.File>)
.subscribe(new Subscriber<File>()
我是rx java的新手,我不知道代码有什么问题。
提前致谢
答案 0 :(得分:2)
在rx-java2
消费者类型已更改。使用io.reactivex.Observer
订阅io.reactivex.Observable
。 org.reactivestreams.Subscriber
仅用于io.reactivex.Flowable
订阅。
.subscribe(new Observer<File>() {
@Override
public void onSubscribe(Subscription s) {
}
@Override
public void onNext(File file) {
}
@Override
public void onError(Throwable t) {
}
@Override
public void onComplete() {
}
});