Khallad
Khallad

Reputation: 121

firebase and google doc to open pdf files

i am trying to open pdf files using google docs and firebase for an android app. The issue i am having is when i use any pdf file from firebase i get this message "No preview available" where if i use any external link it works perfectly fine

Here is the code i used

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_read_pdf);
        path = getIntent().getExtras().getString("pdfPath", "");
        init();
        if(!path.equals("")) {
            Constants.showLoadingDialog(this);
            Log.d("PDFLink", path);
            setPdfView();
        }else{
            Toast.makeText(this, "No PDF to display", Toast.LENGTH_SHORT).show();
        }
    }

    private void init(){
        webview = (WebView) findViewById(R.id.webView);
        backBtn = (ImageView) findViewById(R.id.backBtn);
        backBtn.setOnClickListener(this);
    }

    private void setPdfView() {
        webview.getSettings().setJavaScriptEnabled(true);
        webview.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + path);
        webview.setWebViewClient(new WebViewClient() {

            public void onPageFinished(WebView view, String url) {
                Constants.hideDialog();
            }
        });
    }

This link works "http://www.pdf995.com/samples/pdf.pdf" but when i use any link from firebase it doesnt work. on firebase i tried both "gs://" and "https://".

Upvotes: 0

Views: 1200

Answers (1)

Kalpesh A. Nikam
Kalpesh A. Nikam

Reputation: 323

pdf_url=getArguments().getString("pdf_url");

        final ProgressDialog pDialog = new ProgressDialog(getActivity());
        pDialog.setTitle("Course PDF");
        pDialog.setMessage("Loading...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);

        webView=view.findViewById(R.id.pdfviewr);
        webView.getSettings().setJavaScriptEnabled(true);

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                pDialog.show();
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                pDialog.dismiss();
            }
        });

        String url="";
        try {
            url=URLEncoder.encode(pdf_url,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        webView.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url="+url);

        return view;
    }

//url=URLEncoder.encode(pdf_url,"UTF-8");

Add this to convert URL to UTF-8 And its work fine.

Upvotes: 1

Related Questions