简体   繁体   English

在android应用中打开pdf文件

[英]opening a pdf file in an android app

i am trying to open a pdf file in my app.. i have installed the android pdf viewer in my emulator.. am using the following code.. " https://github.com/jesperborgstrup/buzzingandroid/blob/master/src/com/buzzingandroid/tools/PDFTools.java " 我试图打开我的应用程序PDF文件..我在模拟器安装Android的PDF阅读器..我用下面的代码。“ https://github.com/jesperborgstrup/buzzingandroid/blob/master/src /com/buzzingandroid/tools/PDFTools.java
now i have added the pdf file in my assets folder.. this is my code.. 现在我已经在我的资产文件夹中添加了pdf文件。这是我的代码。

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button;
        final String url = "android.resource://com.buzzingandroid.tools/raw/ll.pdf";
        final PDFTools pdf = new PDFTools();
        button = (Button)this.findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() {

            @SuppressWarnings("static-access")
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                pdf.showPDFUrl(Context,url );
            }
        });

the prg crashes in the "ispdfsupported" module. prg在“ ispdfsupported”模块中崩溃。 the module is given below.. 该模块如下。

public static boolean isPDFSupported( Context context ) {
        Intent i = new Intent( Intent.ACTION_VIEW );
        final File tempFile = new File( context.getExternalFilesDir( Environment.DIRECTORY_DOWNLOADS ), "test.pdf" );
        i.setDataAndType( Uri.fromFile( tempFile ), PDF_MIME_TYPE );
        return context.getPackageManager().queryIntentActivities( i, PackageManager.MATCH_DEFAULT_ONLY ).size() > 0;
    }

and my log cat.. 还有我的原木猫

02-24 01:08:59.912: E/AndroidRuntime(1172): FATAL EXCEPTION: main
02-24 01:08:59.912: E/AndroidRuntime(1172): Process: com.buzzingandroid.tools, PID: 1172
02-24 01:08:59.912: E/AndroidRuntime(1172): java.lang.NullPointerException
02-24 01:08:59.912: E/AndroidRuntime(1172):     at com.buzzingandroid.tools.PDFTools.isPDFSupported(PDFTools.java:142)
02-24 01:08:59.912: E/AndroidRuntime(1172):     at com.buzzingandroid.tools.PDFTools.showPDFUrl(PDFTools.java:38)
02-24 01:08:59.912: E/AndroidRuntime(1172):     at com.buzzingandroid.tools.MAIN$1.onClick(MAIN.java:30)
02-24 01:08:59.912: E/AndroidRuntime(1172):     at android.view.View.performClick(View.java:4438)
02-24 01:08:59.912: E/AndroidRuntime(1172):     at android.view.View$PerformClick.run(View.java:18422)
02-24 01:08:59.912: E/AndroidRuntime(1172):     at android.os.Handler.handleCallback(Handler.java:733)
02-24 01:08:59.912: E/AndroidRuntime(1172):     at android.os.Handler.dispatchMessage(Handler.java:95)
02-24 01:08:59.912: E/AndroidRuntime(1172):     at android.os.Looper.loop(Looper.java:136)
02-24 01:08:59.912: E/AndroidRuntime(1172):     at android.app.ActivityThread.main(ActivityThread.java:5017)
02-24 01:08:59.912: E/AndroidRuntime(1172):     at java.lang.reflect.Method.invokeNative(Native Method)
02-24 01:08:59.912: E/AndroidRuntime(1172):     at java.lang.reflect.Method.invoke(Method.java:515)
02-24 01:08:59.912: E/AndroidRuntime(1172):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-24 01:08:59.912: E/AndroidRuntime(1172):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-24 01:08:59.912: E/AndroidRuntime(1172):     at dalvik.system.NativeStart.main(Native Method)

please explain what i did wrong.. thanks in advance.. 请解释我做错了..在此先感谢..

also i have given an url to access a pdf from server.. it crashed in the same place.. same error in log cat... 我也给了一个URL来从服务器访问一个PDF文件。它在同一地方崩溃了。

Try the below code. 试试下面的代码。 I am using this code for open PDF. 我正在使用此代码来打开PDF。 You can use it for other files also. 您也可以将其用于其他文件。

File file = new File(Environment.getExternalStorageDirectory(),
             "Report.pdf");
    Uri path = Uri.fromFile(file);
    Intent pdfOpenintent = new Intent(Intent.ACTION_VIEW);
    pdfOpenintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    pdfOpenintent.setDataAndType(path, "application/pdf");
    try {
        startActivity(pdfOpenintent);
    } catch (ActivityNotFoundException e) {

    }

If you want to open files. 如果要打开文件。 You can change the setDataAndType(path, "application/pdf") . 您可以更改setDataAndType(path, "application/pdf") If you want to open different files with same intent you can use Intent.createChooser(intent, "Open in..."); 如果要以相同的意图打开不同的文件,则可以使用Intent.createChooser(intent, "Open in..."); . For more information look below link 有关更多信息,请参见下面的链接

How to make an intent with multiple actions 如何通过多种行动表达意图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM