【问题标题】:mupdf: how to open pdf file with openBuffer method?mupdf:如何使用 openBuffer 方法打开 pdf 文件?
【发布时间】:2014-04-28 19:27:33
【问题描述】:

不幸的是,我没有找到任何关于这一点的文档,所以也许我做错了什么。

我使用 android 的 mupdf 示例,并以这种方式稍微修改了 MuPDFActivity 的源代码:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mAlertBuilder = new AlertDialog.Builder(this);

        if (core == null) {
            core = (MuPDFCore)getLastNonConfigurationInstance();

            if (savedInstanceState != null && savedInstanceState.containsKey("FileName")) {
                mFileName = savedInstanceState.getString("FileName");
            }
        }
        if (core == null) {
            Intent intent = getIntent();
            byte buffer[] = null;
            if (Intent.ACTION_VIEW.equals(intent.getAction())) {
                Uri uri = intent.getData();
                try {
                    InputStream inputStream = new FileInputStream(new File(uri.toString()));
                    int len = inputStream.available();
                    buffer = new byte[len];
                    inputStream.read(buffer, 0, len);
                    inputStream.close();
                } catch (Exception e) {
                    Log.e(TAG, e.getMessage());
                }
                if (buffer != null) {
                    core = openBuffer(buffer);
                } else {
                    core = openFile(Uri.decode(uri.getEncodedPath()));
                }
                SearchTaskResult.set(null);
            }

活动的 openBuffer() 方法没有改变:

private MuPDFCore openBuffer(byte buffer[]) {
        System.out.println("Trying to open byte buffer");
        try {
            core = new MuPDFCore(this, buffer);
            // New file: drop the old outline data
            OutlineActivityData.set(null);
        } catch (Exception e) {
            System.out.println(e);
            return null;
        }
        return core;
}

在我尝试打开任何 pdf 文件后,应用程序会显示警告消息“无法打开文档”。这是日志:

03-21 10:43:14.754    3601-3601/com.artifex.mupdfdemo.debug E/libmupdf﹕ Opening document...
03-21 10:43:14.754    3601-3601/com.artifex.mupdfdemo.debug E/libmupdf﹕ error: No document handlers registered
03-21 10:43:14.754    3601-3601/com.artifex.mupdfdemo.debug E/libmupdf﹕ error: Cannot open memory document
03-21 10:43:14.754    3601-3601/com.artifex.mupdfdemo.debug E/libmupdf﹕ Failed: Cannot open memory document

那么,有没有办法将pdf文件作为字节数组打开,这种方式是什么?

【问题讨论】:

    标签: android pdf mupdf


    【解决方案1】:

    我的猜测是您使用的 android jni 文件 mupdf.c 版本比主库文件的版本旧。库 API 发生了变化,需要在创建上下文和调用 fz_open_document 之间调用 fz_register_document_handlers(ctx)。您看到的第一条错误消息表明 fz_register_document_handlers 没有被调用

    【讨论】:

    • 感谢您的回答。问题实际上是没有调用方法 fz_register_document_handlers(ctx) 。但原因不在旧文件中,原因是这个调用只添加在 mupdf.c 的 openFile() 方法中,而不是 openBuffer() :)
    • 前几天已修复,请从 git://git.ghostscript.com/mupdf.git 更新您的来源
    • @aniGem 你能发布你的代码吗?我正在尝试使用 mupdf,我收到错误 java.lang.UnsatisfiedLinkError: dlopen failed: cannot locate symbol "atof" referenced by "libmupdf_java.so"。 .. 你能帮我吗? stackoverflow.com/questions/38343219/…
    猜你喜欢
    • 1970-01-01
    • 2014-02-06
    • 2015-07-20
    • 1970-01-01
    • 2015-07-12
    • 2021-05-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-31
    相关资源
    最近更新 更多