mupdf: how to open pdf file with openBuffer method

2019-06-27 16:25发布

问题:

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