根据@EpicPandaForce在他的评论中给出的提示,我解决了这个问题。我使用Rxjava的create操作符发出一个项目。因此它从未到达onComplete()。根据给出的提示和参考文档,我意识到我必须使用单个。可调用()因为我想发出一根线。
代码:
Single.fromCallable(() ->
{
String s = "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf";
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File folder = new File(extStorageDirectory, "Intelehealth_COVID_PDF");
folder.mkdir();
File pdfFile = new File(folder, "dummy.pdf");
try{
pdfFile.createNewFile();
}catch (IOException e){
e.printStackTrace();
}
FileDownloader.downloadFile(s, pdfFile);
return s;
})
.subscribeOn(Schedulers.io())
.subscribe(new SingleObserver<String>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onSuccess(String s) {
File pdfFile = new File
(Environment.getExternalStorageDirectory()
+ "/Intelehealth_COVID_PDF/" + "dummy.pdf");
Uri path = FileProvider.getUriForFile
(context, context.getApplicationContext().getPackageName()
+ ".provider", pdfFile);
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
pdfIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pdfIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
try{
startActivity(pdfIntent);
}catch(ActivityNotFoundException e){
Toast.makeText(HomeActivity.this, "No Application available to view PDF", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onError(Throwable e) {
}
});