Open online pdf file through android intent?

前端 未结 12 1399
醉话见心
醉话见心 2020-12-04 21:53

Currently I have a pdf url, and I would like to simply using the intent to open it, however, it does not work if I put the url in intent

相关标签:
12条回答
  • 2020-12-04 22:33

    you can view PDF in web view like this

    Intent intent = new Intent(Intent.ACTION_VIEW);
    
    intent.setDataAndType(Uri.parse( "http://docs.google.com/viewer?url=" + pdfLink), "text/html");
    
    startActivity(intent);
    
    0 讨论(0)
  • 2020-12-04 22:37

    you can use the SkewPdfView library for loading pdf from remote urls.

    First of all, Add following in your root build.gradle:

    allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
    

    }

    and add the SkewPdfView Library as:

    dependencies {
        implementation 'com.github.naya-aastra:SkewPdfView:1.1'
    }
    

    Now Include SkewPdfView in your layout:

    <com.nayaastra.skewpdfview.SkewPdfView
        android:id="@+id/skewPdfView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    

    Load a PDF file programmatically as:

    SkewPdfView skewPdfView;
    skewPdfView = findViewById(R.id.skewPdfView);
    skewPdfView.loadPdf(pdfLink);
    

    P.S. link of SkewPdfView library

    SkewPdfView Github Page

    0 讨论(0)
  • 2020-12-04 22:38

    Best practice is wrapping your Intent to Chooser before starting. It provides users with built-in application selection dialog and lets avoid ActivityNotFoundException

    Here a little example:

    Intent browserIntent = new Intent(Intent.ACTION_VIEW);
    browserIntent.setDataAndType(Uri.parse(msg.getData()), Constants.MIME_PDF);
    
    Intent chooser = Intent.createChooser(browserIntent, getString(R.string.chooser_title));
    chooser.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // optional
    
    startActivity(chooser);
    

    Where Constants.MIME_PDF is defined as String value "application/pdf".


    It is possible to ask PackageManager is this Intent has appropriate handling Activity or not.

    public static boolean isActivityForIntentAvailable(Context context, Intent intent) {
        final PackageManager packageManager = context.getPackageManager();
        List list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
        return list.size() > 0;
    }
    
    0 讨论(0)
  • With kotlin using anko, simply call

     context.browse(PDF_URL)
    

    to open any doc file, your browser will redirect you to the doc viewer app.

    If you want to use Google Docs, then append your pdf url as,

    "http://docs.google.com/viewer?url=$PDF_URL"
    
    0 讨论(0)
  • 2020-12-04 22:43

    You can view or download the pdf by either of the two ways i.e by opening it in device in-built browser or in the webview by embedding it in your app.

    To open the pdf in browser,

    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(pdf_url));
    startActivity(browserIntent);
    

    Instead to open in webview,

     Webview webView = (WebView) findViewById(R.id.webView1);
     webView.getSettings().setJavaScriptEnabled(true);
     webView.loadUrl(pdf_url);
    
    0 讨论(0)
  • 2020-12-04 22:43

    you can view pdf in webview like this

    WebView webView = (WebView) findViewById(R.id.webview);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setPluginsEnabled(true);
    webView.loadUrl("https://docs.google.com/viewer?"+pdf_url);
    
    0 讨论(0)
提交回复
热议问题