代码之家  ›  专栏  ›  技术社区  ›  Prajwal Waingankar

onComplete()永远不会在成功下载pdf并尝试在android的pdfview中打开它之后被调用

  •  0
  • Prajwal Waingankar  · 技术社区  · 6 年前

    我试图使用Rxjava2的onNext()从url下载一个pdf文件。在下载并将文件存储在文件夹中之后,我在oncomplete()中编写了代码逻辑,通过意图向用户显示pdf来打开pdfview。但onComplete()永远不会被调用。也使用了断点来检查,但编译器从不执行onComplete()。

    主要活动:

            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");
    //                }
    
    
    
    
                }
            });
    

    文件下载.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执行/工作吗?

    0 回复  |  直到 6 年前
        1
  •  1
  •   Prajwal Waingankar    6 年前

    根据@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) {
    
                            }
                        });