Samaludheen
Samaludheen

Reputation: 176

Cannot resolve method 'subscribe(anonymous org.reactivestreams.Subscriber<java.io.File>)

I am trying to download and send pdf using Intent to PDF app to display the file as seen here as the answer of JDenais This is the code to download the pdf and pass it via 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() {

                }
            });
}
}

But I am getting the error

'Cannot resolve method subscribe(anonymous org.reactivestreams.Subscriber<java.io.File>) on .subscribe(new Subscriber<File>()

I am new to rx java, I don't know what is wrong with code.

Thanks in advance

Upvotes: 1

Views: 2631

Answers (1)

mol
mol

Reputation: 2677

In rx-java2 consumer type changed. Use io.reactivex.Observer to subscribe to io.reactivex.Observable. org.reactivestreams.Subscriber is used only for io.reactivex.Flowable subscriptions.

.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() {

        }
    });

Upvotes: 4

Related Questions