Я пытаюсь загрузить PDF-файл с URL-адреса, используя onNext () из Rxjava2. Загрузив и сохранив файл в папке, я написал код logi c в oncomplete (), чтобы открыть pdf-просмотр через намерение показать pdf пользователю. Но onComplete () никогда не вызывается. Также использовали контрольные точки для проверки, но компилятор никогда не выполняет onComplete ().
MainActivity:
home_quarantine_guidelines.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
observable = Observable.just
("https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf");
observable.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new DisposableObserver<String>() {
@Override
public void onNext(String s) {
//customProgressDialog.show();
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);
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
customProgressDialog.dismiss();
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();
}
}
});
// File pdfFile_downloaded = new File(Environment.getExternalStorageDirectory() + "/Intelehealth_COVID_PDF/" + "dummy.pdf");
//
// if(pdfFile_downloaded.exists())
// {
// File pdfFile = new File(Environment.getExternalStorageDirectory() + "/Intelehealth_COVID_PDF/" + "dummy.pdf"); // -> filename = maven.pdf
// //Uri path = Uri.fromFile(pdfFile);
// 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();
// }
// }
// else
// {
// customProgressDialog.show();
// new DownloadFile().execute("https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf", "dummy.pdf");
// }
}
});
FileDownloaded.class:
public class FileDownloader extends FileProvider {
private static final int MEGABYTE = 1024 * 1024;
public static void downloadFile(String fileUrl, File directory){
try {
URL url = new URL(fileUrl);
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
// urlConnection.setRequestMethod("GET");
// urlConnection.setDoOutput(true);
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream fileOutputStream = new FileOutputStream(directory);
int totalSize = urlConnection.getContentLength();
byte[] buffer = new byte[MEGABYTE];
int bufferLength = 0;
while((bufferLength = inputStream.read(buffer)) > 0){
fileOutputStream.write(buffer, 0, bufferLength);
}
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
РЕДАКТИРОВАТЬ: Когда я использую AsyncTask, код выполняется правильно. Но почему он не выполняется / не работает с использованием RxJava2?