Mupdf: how to open a pdf file using the openBuffer method?

Unfortunately, I did not find any documentation on this, so maybe something is wrong with me.

I am using the mupdf sample for android, and I slightly modified the source code of MuPDFActivity as follows:

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

Failed to change openBuffer () method:

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 file, the application displays a warning with the message "Cannot open the document." These are the 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, is there a way to open a pdf file as a byte array and what is it?

+4
source share
1 answer

, android jni mupdf.c, . API , - fz_register_document_handlers (ctx) fz_open_document. , , , fz_register_document_handlers

+5

Source: https://habr.com/ru/post/1532824/


All Articles