mupdf: how to open pdf file with openBuffer method?

我怕爱的太早我们不能终老 提交于 2019-12-10 15:39:57

问题


Unfortunatelly, I haven't found any documentation on this point, so maybe I do something wrong.

I use mupdf sample for android and I have slightly modified the source code of MuPDFActivity this way:

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);
            }

The activity's openBuffer() method hasn't been changed:

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;
}

After I try to open any pdf, app shows alert with message "Cannot open document". This is logs:

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

So, whether there is any way to open pdf file as a byte array and what is this way?


回答1:


My guess is that you are using a version of the android jni file mupdf.c that is older than the version of the main library files. There was a change in the library API which necessitates fz_register_document_handlers(ctx) being called between creating the context and calling fz_open_document. The first error message you are seeing suggests that fz_register_document_handlers is not being called



来源:https://stackoverflow.com/questions/22558597/mupdf-how-to-open-pdf-file-with-openbuffer-method

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!