【问题标题】:opening a pdf file in an android app在 Android 应用中打开 pdf 文件
【发布时间】:2014-02-25 13:48:21
【问题描述】:

我正在尝试在我的应用程序中打开一个 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.. 它在同一个地方崩溃了.. log cat 中的同样错误...

【问题讨论】:

    标签: android pdf


    【解决方案1】:

    试试下面的代码。我正在使用此代码打开 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

    【讨论】:

    • 但他用的是PDFTools。检查一下
    猜你喜欢
    • 2016-04-05
    • 2013-07-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-10
    • 2013-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多