gpt4 book ai didi

java - 使用 MuPDF 阅读在线 PDF 文件

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:23:58 31 4
gpt4 key购买 nike

我有两个问题1)注释不起作用2) 我想阅读在线pdf文档

我使用此链接构建 MuPDF http://mupdf.com/doc/how-to-build-mupdf-for-android

1) 我得到项目 mupdf/platform/android我运行该默认应用程序注释工作正常,但我将该项目创建到他们要求保存的库文件中,但注释未保存。

2) 它将读取本地文件,但我也想读取在线文件。我的安卓代码

pdf = (TextView) findViewById(R.id.pdf);
onPdf = (TextView) findViewById(R.id.onPdf);

pdf.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

Uri uri = Uri.parse(path);

Intent intent = new Intent(context, MuPDFActivity.class);

intent.setAction(Intent.ACTION_VIEW);

intent.setData(uri);

startActivity(intent);


}
});

onPdf.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

Uri uri = Uri.parse(netPath);


Intent intent = new Intent(context, MuPDFActivity.class);

intent.setAction(Intent.ACTION_VIEW);

intent.setData(uri);

startActivity(intent);

}
});

修改 MuPDFActivity - 我更改所有文件都打开 openBuffer 方法

private MuPDFCore openBuffer(byte buffer[]) {
System.out.println("Trying to open byte buffer");
try {
String magic = "";
core = new MuPDFCore(this, buffer, magic);
// New file: drop the old outline data
OutlineActivityData.set(null);
} catch (Exception e) {
System.out.println(e);
return null;
}
return core;
}

我的主菜

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".PdfTestActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.artifex.mupdfdemo.MuPDFActivity"
android:label="@string/app_name"
android:theme="@style/AppBaseTheme" >
</activity>

<activity
android:name="com.artifex.mupdfdemo.OutlineActivity"
android:label="@string/outline_title"
android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen" >
</activity>
<activity
android:name="com.artifex.mupdfdemo.PrintDialogActivity"
android:label="@string/print"
android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen" >
</activity>
</application>

我的堆栈跟踪

12-25 19:02:41.544: E/TAG(22925): /sample.pdf:     open failed: ENOENT (No such file or directory)
12-25 19:02:41.544: E/tag(22925): file
12-25 19:02:41.546: I/System.out(22925): Trying to open /sample.pdf
12-25 19:02:41.549: D/dalvikvm(22925): Trying to load lib /mnt/asec/com.example.testnew-1/lib/libmupdf.so 0x41792e10
12-25 19:02:41.576: D/dalvikvm(22925): Added shared lib /mnt/asec/com.example.testnew-1/lib/libmupdf.so 0x41792e10
12-25 19:02:41.576: D/dalvikvm(22925): No JNI_OnLoad found in /mnt/asec/com.example.testnew-1/lib/libmupdf.so 0x41792e10, skipping init
12-25 19:02:41.577: I/libmupdf(22925): Opening document...
12-25 19:02:41.577: E/libmupdf(22925): error: cannot open /sample.pdf: No such file or directory
12-25 19:02:41.577: E/libmupdf(22925): error: cannot load document '/sample.pdf'
12-25 19:02:41.577: E/libmupdf(22925): error: Cannot open document: '/sample.pdf'
12-25 19:02:41.577: E/libmupdf(22925): Failed: Cannot open document: '/sample.pdf'
12-25 19:02:41.580: I/System.out(22925): java.lang.Exception: Cannot open file: /sample.pdf

最佳答案

我下载 PDF 文件并转换为字节并在 MuPDF Core 中打开。

下载文件

byte buffer[] = null;
HttpURLConnection urlConnection = null;
try {

urlConnection = (HttpURLConnection) urls[0].openConnection();
urlConnection.setReadTimeout(TIMEOUT_READ);
InputStream inputStream = urlConnection.getInputStream();

int len = inputStream.available();
buffer = new byte[len];

// inputStream.read(buffer, 0, len);

while ((len = inputStream.read(buffer)) != -1) {
byteBuffer.write(buffer, 0, len);
}
Log.e("Length", "" + byteBuffer.size());
inputStream.close();
// and then we can return your byte array.

} catch (Exception e) {
e.printStackTrace();

});

开放核心

RelativeLayout mupdfWrapper;
MuPDFCore core;
public byte[] buffer;
mupdfWrapper = (RelativeLayout) viewRoot
.findViewById(R.id.mupdf_wrapper);
// Intent intent =new Intent(getActivity(),MuPDFActivity.class);
// startActivity(intent);

core = new MuPDFCore(getActivity(), buffer, "");
MuPDFReaderView mDocView = new MuPDFReaderView(getActivity());
mDocView.setAdapter(new MuPDFPageAdapter(getActivity(), core));
mupdfWrapper.addView(mDocView, new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

关于java - 使用 MuPDF 阅读在线 PDF 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34463296/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com