5

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?

aniGem
  • 53
  • 1
  • 4

1 Answers1

6

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

Paul Gardiner
  • 468
  • 2
  • 7
  • 1
    Thanks for your answer. The problem is really that the method fz_register_document_handlers(ctx) is not called. But cause is not in old files, cause is that this calling was added only in openFile() method of mupdf.c, not in openBuffer() :) – aniGem Mar 24 '14 at 08:10
  • Was fixed few day ago, update your sources from git://git.ghostscript.com/mupdf.git – iBog Jun 25 '14 at 14:32
  • @aniGem can you post your code I am trying to use mupdf and I am getting an error java.lang.UnsatisfiedLinkError: dlopen failed: cannot locate symbol "atof" referenced by "libmupdf_java.so"... can you help? http://stackoverflow.com/questions/38343219/how-to-view-a-pdf-file-in-android-using-mupdf/38343357#38343357 – AndroidNewBee Jul 13 '16 at 05:55