可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am working on some stuff that should be able to read PDF in my app and I want to put PDF view in my custom layout. I had preferred Android PDF Viewer but when I performed zoomIn, zoomOut it takes too much time.
So currently I am supposed to use MuPDF open source project to integrate in my project, it's based on JNI and I am not used to it.
I am using Cygwin to build the library for native code. Hence I am unclear with few things:
how to integrate the MuPDF in my project (as per my question title)?
once I will succeed to integrated it then how can I put PDF reader in my custom view (in the XML or programmaticaly)?
回答1:
I don't know how to do this in Windows using cygwin, because I'm using Ubuntu for the development. But I think the procedure should be the same.
- Download the file mupdf-0.9-source.tar.gz here: http://code.google.com/p/mupdf/downloads/list?q=source
- Download the file mupdf-thirdparty.zip
- Extract the sources. By default they will be extracted to the folder: mupdf-0.9/
- Extract the file mupdf-thirdparty.zip into the folder mupdf-0.9/
- Build the project mupdf-0.9 (For windows you should use VS as it is declared in the readme files)
- Then go to the folder mupdf-0.9/android/
- Run ndk-build
- You can get the following errors:
Compile thumb : mupdfthirdparty
The solution is explained here: mupdf for android: ndk-build problem (error: redefinition of typedef....) However, you can simply comment the lines of the definition of types in the file /thirdparty/jbig2dec/os_types.h
After that you will receive two libraries: one static and one shared for your android application.
StaticLibrary : libmupdfthirdparty.a SharedLibrary : libmupdf.so
This was the answer on the first question. Also there is a great step-by-step guide in the android/Readme.txt file.
Now the answer on the second question. In the application for android you can find test project. There are 3 files:
- MuPDFActivity.java
- MuPDFCore.java
- PixmapView.java
Simply copy the last two files in your project. And see an example in MuPDFActivity.java how you can embed mupdf layout in your activity. In this file it is done like:
PixmapView pixmapView; //... layout = new RelativeLayout(this); //... RelativeLayout.LayoutParams pixmapParams = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT); pixmapParams.addRule(RelativeLayout.ABOVE,100); layout.addView(pixmapView, pixmapParams); setContentView(layout);
回答2:
回答3:
This is How I achieve on my mac (2012, intel i5):
Step 1 : Get mupdf-1.2-source.zip
Step 2 : Get android-ndk-mac-64
Step 3 : unzip both of them in new folder call Android-pdf
and rename unzip folder to mupdf
and android-ndk
(you can call them whatever you like)
Step 4 : open Terminal and use command : cd
until you are in android-pdf
folder
Step 5 : cd mupdf
than command: make
(will take about 40 sec. to run all scripts)
Step 6 : cd android
(within mupdf
dir.)
Step 7 : open finder go to folder Android-pdf
that you created than android-ndk
drag file call ndk-build
into terminal command line and enter (basically adding a path to ndk-build to operate on mupdf lib.)
and after few sec. you should have new folder inside mupdf > android > libs > ...
use that in your android project.
How to use MuPDF with your EXISTING Eclipse project:
- Copy the 'jni' folder from the /android folder into your existing Eclipse project.
- Copy the /thirdparty folder into the 'jni' folder in your project.
- Copy the /cbz folder into the 'jni' folder in your project.
- Copy the /draw folder into the 'jni' folder in your project.
- Copy the /fitz folder into the 'jni' folder in your project.
- Copy the /generated folder into the 'jni' folder in your project.
- Copy the /pdf folder into the 'jni' folder in your project.
- Copy the /scripts folder into the 'jni' folder in your project.
- Copy the /xps folder into the 'jni' folder in your project.
- Open 'Android.mk' inside the 'jni' folder.
Change
MUPDF_ROOT := ..
to
MUPDF_ROOT := $(TOP_LOCAL_PATH)
- Save 'Android.mk'.
- Open 'Core.mk' inside the 'jni' folder.
Change
MY_ROOT := ../..
to
MY_ROOT := $(LOCAL_PATH)
Change all the
..
in LOCAL_C_INCLUDES to
$(LOCAL_PATH)
- Save 'Core.mk'.
- Open 'ThirdParty.mk' inside the 'jni' folder.
Change
MY_ROOT := ../..
to
MY_ROOT := $(LOCAL_PATH)
Change all the
..
in LOCAL_C_INCLUDES to
$(LOCAL_PATH)
- Save 'ThirdParty.mk'.
- Now execute 'ndk-build' in your project's 'jni' directory.
- Copy everything in the /android/src folder into the 'src' folder in your project.
- Copy everything in the /android/res/drawable folder into the 'res/drawable' folder in your project.
- Copy everything in the /android/res/drawable-ldpi folder into the 'res/drawable-ldpi' folder in your project.
- Copy everything in the /android/res/drawable-mdpi folder into the 'res/drawable-mdpi' folder in your project.
- Copy everything in the /android/res/layout folder EXCEPT main.xml (because if you are copying into an existing project then you should already have your own main.xml or equivalent) into the 'res/layout' folder in your project.
- Copy everything in the /android/res/values folder into the 'res/values' folder in your project. If you already have a 'strings.xml' in your existing project, copy everything in between the '' tags in your /android/res/values/strings.xml into your project's strings.xml (paste between the '' tags). Similarly with the 'colors.xml', if you already have a 'colors.xml' in your existing project, copy everything in between the '' tags in your /android/res/values/strings.xml into your project's strings.xml (paste between the '' tags).
- Open the 'AndroidManifest.xml' in project.
In between the '' tags paste all the list of activities from
AndroidManifest.xml
inside the /android folder, You should copy from your working copy of AndroidManifest.xml inside mupdf.
MuPDF in now in your existing Eclipse project. To use it, call up com.artifex.mupdf.ChoosePDFActivity.class
in your application. This is the main class for MuPDF.
To open pdf with pre-fix file:
Uri uri = Uri.parse("path to pdf file");
Intent intent = new Intent(context, MuPDFActivity.class);
intent.setAction(Intent.ACTION_VIEW);
intent.setData(uri);
context.startActivity(intent);
Hope this help :)
回答4:
Check this project eBookDroid
EBookDroid is an open source (GPL'ed) document viewer for Android based on the VuDroid code base.
Supported the file in following formats:
PDF DjVu XPS (OpenXPS) comics books (cbz,cbr)
回答5:
I have view pager in my application. I need to show pdf files on each fragment in view pager.is there way to get a pdf view from mupdf
? normally we call mupdf
as below
Uri uri = Uri.parse("path to pdf file"); Intent intent = new Intent(context, MuPDFActivity.class); intent.setAction(Intent.ACTION_VIEW); intent.setData(uri); context.startActivity(intent);
So if i use mupdf
for my app i have to call MuPDFActivity
on each fragment.I think its not a correct way?
回答6:
The answer given by star18bit
pretty much sums it up, but even folllowing that I faced a lot of issues.
Like the make
command didn't work for me and I had to build it using Visual Studio. So I listed all the pre-req softwares. Like ANT. Also we need to add the sdk folder's tools
and platform-tools
folders in PATH.
Also, I had to do the changes in Core2.mk
file, the same ones in Core.mk
. I can't give all the information here, cause it is a lot, so I referring this link for complete information:
http://howtojava.net/tutorials/android/integrate-mupdf-with-android-in-windows-7