0

i have one pdf file in internal storage of my device using below code to view pdf this code is working in all version of android except nougat.

Class to generate Pdf is as follows :

        File dir = new File(Environment.getExternalStorageDirectory(), filepath);
        if(!dir.exists()) {

            dir.mkdir();
        }
        filename = "O"+pref.getCurrentOdrerId()+".pdf";
        System.out.println("filename: "+filename);
        pdfFile = new File(dir.getAbsoluteFile()+"/"+filename);

        Uri uri;
        int currentapiVersion = android.os.Build.VERSION.SDK_INT;
        if (currentapiVersion >= Constants.ANDROID_NOUGAT) {
            //uri = Uri.parse(pdfFile.getAbsolutePath());
            uri = FileProvider.getUriForFile(getActivity(), BuildConfig.APPLICATION_ID + ".provider",
                    new File(pdfFile.getAbsolutePath()));
        }
        else{
            uri = Uri.fromFile(new File(pdfFile.getAbsolutePath()));
        }
        WebView webView = (WebView) v.findViewById(R.id.showInvoicePdf);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setDomStorageEnabled(true);
        webView.setWebViewClient(new MyBrowser());
        webView.loadUrl(uri.toString());//String.valueOf(new File(pdfFile.getAbsolutePath()))

Any help is appreciated!

Rinzin
  • 21
  • 7
  • Please remove all irrelevant code. Five code lines would have been enough to demonstrate your problem. Further you did not tell what your problem is and there is no question. Please dont just dump code here. Behave. – greenapps May 18 '17 at 07:06
  • @greenapps It will be very helpful if you can help me in solving the problem . Hope the edit clears my question. Any help is deeply appreciated – Rinzin May 18 '17 at 07:23
  • It would be very helpfull if you removed all irrelevant code first. – greenapps May 18 '17 at 07:26
  • i removed all the irrelevant code – Rinzin May 18 '17 at 07:40
  • 1
    You did not mention that you are trying to load a pdf file in a WebView. A WebView cannot display a pdf file. – greenapps May 18 '17 at 07:46
  • If there is already a pdf file in a dir then calling mkdir does not make sense. Isnt it? – greenapps May 18 '17 at 07:48
  • Besides of that i see no reason to use a file provider for nougat. That would only be needed if you used an external app to display the pdf. Well... you have to use an external app now, anyhow. – greenapps May 18 '17 at 07:50
  • http://stackoverflow.com/questions/44016169/ – greenapps May 18 '17 at 07:54
  • @greenapps Thanks finally solved using GitHub.PdfViewer library. – Rinzin May 18 '17 at 10:19
  • PLease refer below link: https://stackoverflow.com/questions/4947591/open-a-pdf-file-inside-a-webview/6040247#6040247 – Alok Gupta Apr 01 '19 at 13:18

0 Answers0