2014-02-24 42 views
0

我想在我的应用程序中打开PDF文件..我已经在我的模拟器中安装了android pdf查看器..正在使用以下代码..“https://github.com/jesperborgstrup/buzzingandroid/blob/master/src/com/buzzingandroid/tools/PDFTools.java
now i添加PDF文件在我的资产文件夹..这是我的代码..在Android应用程序中打开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); 
      } 
     }); 

的“ispdfsupported”模块中的PRG崩溃。该模块如下..

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 ..它坠毁在同一个地方..日志猫相同的错误...

+0

张贴您的logcat。 –

回答

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

+0

但他使用PDFTools。检查它 –

相关问题