Open online pdf file through android intent?

2019-01-14 17:03发布

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

My code is like this, it always throw ActivityNotFoundException error

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(law.url), "application/pdf");

try {
    startActivity(intent);
} catch (ActivityNotFoundException e){
    Utility.showErrorDialog(
        ctx,ctx.getResources().getString(R.string.sys_in, 
        ctx.getResources().getString(R.string.err_no_pdf_reader), 
        ctx.getResources().getString(R.string.close));
}

Also I tried the goolge doc approach but my client reject this, so I am not using this method

Intent intent = new Intent(Intent.ACTION_VIEW);

intent.setDataAndType(Uri.parse("http://docs.google.com/viewer?url=" 
+ publish.get(Integer.parseInt((String) view.getTag())).pdfURL), "text/html");  
ctx.startActivity(intent);

Thanks for help

Log cat error Update

04-23 18:18:50.487: E/AndroidRuntime(17161): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=http://oshc.zizsoft.com/oshc_testing.pdf typ=application/pdf }
04-23 18:18:50.487: E/AndroidRuntime(17161):    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1568)
04-23 18:18:50.487: E/AndroidRuntime(17161):    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1439)
04-23 18:18:50.487: E/AndroidRuntime(17161):    at android.app.Activity.startActivityForResult(Activity.java:3356)
04-23 18:18:50.487: E/AndroidRuntime(17161):    at android.app.Activity.startActivityForResult(Activity.java:3317)
04-23 18:18:50.487: E/AndroidRuntime(17161):    at android.support.v4.app.FragmentActivity.startActivityFromFragment(FragmentActivity.java:848)
04-23 18:18:50.487: E/AndroidRuntime(17161):    at android.support.v4.app.Fragment.startActivity(Fragment.java:878)
04-23 18:18:50.487: E/AndroidRuntime(17161):    at com.example.oshpedia.Fragment.Shelf$4.onItemClick(Shelf.java:142)
04-23 18:18:50.487: E/AndroidRuntime(17161):    at it.sephiroth.android.library.widget.AdapterView.performItemClick(AdapterView.java:299)
04-23 18:18:50.487: E/AndroidRuntime(17161):    at it.sephiroth.android.library.widget.AbsHListView.performItemClick(AbsHListView.java:972)
04-23 18:18:50.487: E/AndroidRuntime(17161):    at it.sephiroth.android.library.widget.AbsHListView$PerformClick.run(AbsHListView.java:2511)
04-23 18:18:50.487: E/AndroidRuntime(17161):    at it.sephiroth.android.library.widget.AbsHListView$1.run(AbsHListView.java:3200)
04-23 18:18:50.487: E/AndroidRuntime(17161):    at android.os.Handler.handleCallback(Handler.java:615)
04-23 18:18:50.487: E/AndroidRuntime(17161):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-23 18:18:50.487: E/AndroidRuntime(17161):    at android.os.Looper.loop(Looper.java:137)
04-23 18:18:50.487: E/AndroidRuntime(17161):    at android.app.ActivityThread.main(ActivityThread.java:4882)
04-23 18:18:50.487: E/AndroidRuntime(17161):    at java.lang.reflect.Method.invokeNative(Native Method)
04-23 18:18:50.487: E/AndroidRuntime(17161):    at java.lang.reflect.Method.invoke(Method.java:511)
04-23 18:18:50.487: E/AndroidRuntime(17161):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
04-23 18:18:50.487: E/AndroidRuntime(17161):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
04-23 18:18:50.487: E/AndroidRuntime(17161):    at dalvik.system.NativeStart.main(Native Method)

7条回答
等我变得足够好
2楼-- · 2019-01-14 17: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);
查看更多
Juvenile、少年°
3楼-- · 2019-01-14 17:37

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);
查看更多
劫难
4楼-- · 2019-01-14 17:39

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);

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;
}
查看更多
Deceive 欺骗
5楼-- · 2019-01-14 17:41

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);
查看更多
Explosion°爆炸
6楼-- · 2019-01-14 17:50

Download source code from here (Open Pdf from url in Android Programmatically )

MainActivity.java

package com.deepshikha.openpdf;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

public class MainActivity extends AppCompatActivity {
    WebView webview;
    ProgressBar progressbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webview = (WebView)findViewById(R.id.webview);
        progressbar = (ProgressBar) findViewById(R.id.progressbar);
        webview.getSettings().setJavaScriptEnabled(true);
        String filename ="http://www3.nd.edu/~cpoellab/teaching/cse40816/android_tutorial.pdf";
        webview.loadUrl("http://docs.google.com/gview?embedded=true&url=" + filename);

        webview.setWebViewClient(new WebViewClient() {

            public void onPageFinished(WebView view, String url){
                // do your stuff here
                progressbar.setVisibility(View.GONE);
            }
        });

    }
}
查看更多
贪生不怕死
7楼-- · 2019-01-14 17:50

The actual error

android.content.ActivityNotFoundException: No Activity found to 
handle Intent 
{
 act=android.intent.action.VIEW 
 dat=http://oshc.zizsoft.com/oshc_testing.pdf typ=application/pdf 
}

This says that:

  1. You "broadcast" and Intent to let the system try to open a PDF file
  2. The system does not find any application registered to be able to handle this type of file (PDF)

You just need a PDF viewer of some kind.

Solution

Get a PDF reader app or use @Mahendra's solution.

查看更多
登录 后发表回答