在Android应用程序中打开pdf文件

时间:2014-02-24 06:36:50

标签: android pdf

我想在我的应用程序中打开一个pdf文件..我已经在我的模拟器中安装了android pdf查看器..我正在使用以下代码..“https://github.com/jesperborgstrup/buzzingandroid/blob/master/src/com/buzzingandroid/tools/PDFTools.java

 现在我已经在我的资源文件夹中添加了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 );
            }
        });

prg在“ispdfsupported”模块中崩溃。该模块如下所示。

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

和我的日志猫..

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)

请解释我做错了什么..提前谢谢..

我也给了一个从服务器访问pdf的URL ..它在同一个地方坠毁..日志猫中的错误......

1 个答案:

答案 0 :(得分:0)

尝试以下代码。我正在使用此代码打开PDF。您也可以将它用于其他文件。

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

    }

如果要打开文件。您可以更改setDataAndType(path, "application/pdf")。如果要以相同的意图打开不同的文件,可以使用Intent.createChooser(intent, "Open in...");。有关更多信息,请参阅以下链接

How to make an intent with multiple actions

相关问题