以编程方式打开pdf文件 [英] open a pdf file programmatically

查看:103
本文介绍了以编程方式打开pdf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究pdf.我正在尝试使用以下代码从我的应用程序打开pdf文件.但是我没打开.

I am working on pdf. I am trying to open a pdf file from my application using the code below. But I failed to open.

private void openPdf() {

        File file = new File("mnt/sdcard.test.pdf");
        Uri path = Uri.fromFile(file);
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.setAction(Intent.ACTION_VIEW);
        intent.setData(path);
        intent.setType("application/pdf");
        try {
            startActivity(intent);
        } catch (ActivityNotFoundException e) {
            Toast.makeText(getActivity(), "No application found",
                    Toast.LENGTH_SHORT).show();
        }
    }

当我在模拟器中尝试此代码时,它显示了一个吐司,说找不到应用程序"(bcoz,通常在模拟器中未安装pdf查看应用程序).当我在设备(特别是在funbook标签和sony标签)中测试相同的东西时,它既没有显示Toast消息,也没有打开pdf文件.任何人都可以指出我的代码中的错误.实际上,我是第一次使用pdf.所以我的问题是,

When I tried this code in emulator,it shows a toast saying "No application found"(bcoz,normally no pdf viewing application are installed in emulator). When I tested the same thing in device (specifically in funbook tab and in sony tab), it neither showed the Toast message nor it opened the pdf file. Can anybody point out the mistake in my code. Actually I am working with pdf for the first time. So my question is,

  1. 在设备中没有显示吐司消息,这意味着有一个在手机中安装了pdf查看应用程序?是吗?
  2. 如果是,为什么不使用第三方应用程序打开pdf.
  3. 如果我想列出我安装的所有pdf查看应用程序电话给用户,我应该对这段代码进行哪些更改?

推荐答案

我已经解决了上述问题,因此请尝试一次;

I got solution of above problem, so try once;

步骤:-

  1. 在src中以您的应用程序名称创建资产文件夹.

  1. create assets folder in src under your app name.

在此资产文件夹中,保存您的pdf文件,例如schedule1.pdf.

In this assets folder keep your pdf files e.g. schedule1.pdf.

现在来参加您的活动,即MainActivity.java

now come your activity i.e MainActivity.java

setListener,即( Button ImageView ImageButton );

setListener on any UI component what you want i.e (Button, ImageView, ImageButton);

在此侦听器中,调用一个用户定义的方法,即 openPDFFiles()

In this listener call one user defined method i.e. openPDFFiles()

openPDFFiles()方法具有以下代码:

the openPDFFiles() method have below code:

private void openPDFFiles() {
    AssetManager assetManager = getAssets();

    InputStream in = null;
    OutputStream out = null;
    File file = new File(getFilesDir(), "schedule1.pdf");//here schedule1.pdf is the pdf file name which is keep in assets folder.
    try {
        in = assetManager.open("schedule1.pdf");
        out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);

        copyFile(in, out);
        in.close();
        in = null;
        out.flush();
        out.close();
        out = null;
    } catch (Exception e) {
        Log.e("tag", e.getMessage());
    }

    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.parse("file://" + getFilesDir() + "/schedule1.pdf"), "application/pdf");

    startActivity(intent);
}

private void copyFile(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    int read;
    while ((read = in.read(buffer)) != -1) {
        out.write(buffer, 0, read);
    }
}

这篇关于以编程方式打开pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆