android - 如何从 assets 文件夹中打开 Android 中的 PDF 文件?

标签 android pdf

有没有人知道如何在 Android 中打开 PDF 文件?我的代码看起来是这样的:

public class SampleActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        CopyReadAssets();    
    }

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

        InputStream in = null;
        OutputStream out = null;
        File file = new File(getFilesDir(), "git.pdf");
        try {
            in = assetManager.open("git.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() + "/git.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);
        }
    }
}

最佳答案

这是从 Assets 文件夹打开 pdf 文件的代码,但您必须在设备上安装 pdf 阅读器:

    private void CopyAssets() {

        AssetManager assetManager = getAssets();

        InputStream in = null;
        OutputStream out = null;
        File file = new File(getFilesDir(), "fileName.pdf");
        try {
            in = assetManager.open("fileName.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() + "/fileName.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);
        }
    }

关于android - 如何从 assets 文件夹中打开 Android 中的 PDF 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12889608/

相关文章:

android - 无法使用 NotificationListenerService 取消通知

python - 同一个图中的多个 matplotlib 图 + 到 pdf-Python

ASP.NET PDF 查看器

android - 如何确定 Android 是否可以处理 PDF

Android - 从 Emojione 到 Unicode 的短名称不起作用

android - 位图压缩不会改变位图字节大小

android - 从 Fragment 处理 OnBackPress

android - 使用 ImageReader 后,在三星 Note5 上录制的视频无法与 Camera2 API 一起使用

java - PDF签名 itext pkcs7 多重签名

pdf - 使用ByteArrayOutputStream使用iText将水印添加到pdf