Android : how to open pdf file from server in android [closed]

天涯浪子 提交于 2019-11-27 03:42:18

问题


I'm new to android programming and I'm developing an application in android to open a PDF file from a server in my app. I've no idea about that. So please help me. How i can do this?

Thanks in advance.


回答1:


you have to try this code for open a pdf file in your application..

WebView webView = (WebView) findViewById(R.id.my_webview);
webView.setWebViewClient(new MyWebViewClient());
webView.addView(webView.getZoomControls());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://docs.google.com/gview?embedded=true&url=http://myurl.com/demo.pdf");



回答2:


   String pdf = "http://www.xyzwebsite.com/yourfile.pdf"; //YOUR URL TO PDF
   String googleDocsUrl = "http://docs.google.com/viewer?url="+ pdfurl;
   Intent intent = new Intent(Intent.ACTION_VIEW);
   intent.setDataAndType(Uri.parse(googleDocsUrl ), "text/html");
   startActivity(intent);

Add this permission to manifest file

<uses-permission android:name="android.permission.INTERNET" >



回答3:


File file = new File("/sdcard/example.pdf");

                if (file.exists()) {
                    Uri path = Uri.fromFile(file);
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setDataAndType(path, "application/pdf");
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                    try {
                        startActivity(intent);
                    } 
                    catch (ActivityNotFoundException e) {
                        Toast.makeText(OpenPdf.this, 
                            "No Application Available to View PDF", 
                            Toast.LENGTH_SHORT).show();
                          String pdf = "http://www.xyzwebsite.com/yourfile.pdf"; //YOUR URL TO PDF
                          String googleDocsUrl = "http://docs.google.com/viewer?url="+ pdfurl;
                          Intent intent = new Intent(Intent.ACTION_VIEW);
                          intent.setDataAndType(Uri.parse(googleDocsUrl ), "text/html");
                          startActivity(intent);


                    }
                }
            }


来源:https://stackoverflow.com/questions/11718640/android-how-to-open-pdf-file-from-server-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!