我尝试了下面的代码,该代码使用PDF.js(检查此answer for reference)在android上打开PDF文件,但尽管事实上它在Oneplus3上使用Android N和Marshmallow设备工作正常但它无法使用Lollypop或其他具有ICS或Jelly Bean的设备(使用Genymotion测试)在某些三星设备(我在三星Galaxy S4上测试)上呈现悬挂问题。挂起问题是如此糟糕,以至于我没有看到任何来自设备的日志,我可以摆脱挂起问题是取出电池并重新打开设备。 MuPDF PDF查看器也会出现此问题。我想知道问题的解决方案。
代码:
webView = (WebView) findViewById(R.id.webView1);
final WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
//
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN) //required for running javascript on android 4.1 or later
{
settings.setAllowFileAccessFromFileURLs(true);
settings.setAllowUniversalAccessFromFileURLs(true);
}
settings.setBuiltInZoomControls(true);
webView.setWebChromeClient(new WebChromeClient());
Uri path = Uri.parse("file:///android_asset/test.pdf");
try {
InputStream ims = getAssets().open("pdfviewer/index.html");
String line = getStringFromInputStream(ims);
if(line.contains("THE_FILE")) {
line = line.replace("THE_FILE", path.toString());
FileOutputStream fileOutputStream = openFileOutput("index.html", Context.MODE_PRIVATE);
fileOutputStream.write(line.getBytes());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
webView.loadUrl("file://" + getFilesDir() + "/index.html");
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
return true;
case R.id.action_next:
webView.loadUrl("javascript:onNextPage()");
return super.onOptionsItemSelected(item);
case R.id.action_previous:
webView.loadUrl("javascript:onPrevPage()");
return super.onOptionsItemSelected(item);
default:
return super.onOptionsItemSelected(item);
}
}
// convert InputStream to String
private static String getStringFromInputStream(InputStream is) {
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
String line;
try {
br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sb.toString();
}
这个耗费大量内存的浏览器或Javascript是问题的原因吗?鉴于上述方法所采用的理想小应用程序大小以及如果上述问题得到修复支持它可以给旧设备哪些PDFRenderer(后来在Android API版本中引入并产生安全异常)不能,我发现这种查看PDF文件的方法理想
Log Cat输出:
08-17 13:26:53.436 30828-30828/securitymsg.pdfjsviewer E/Zygote: MountEmulatedStorage()
08-17 13:26:53.436 30828-30828/securitymsg.pdfjsviewer E/Zygote: v2
08-17 13:26:53.436 30828-30828/securitymsg.pdfjsviewer I/SELinux: Function: selinux_compare_spd_ram, SPD-policy is existed. and_ver=SEPF_GT-I9500_5.0.1 ver=51
08-17 13:26:53.436 30828-30828/securitymsg.pdfjsviewer I/libpersona: KNOX_SDCARD checking this for 10319
08-17 13:26:53.436 30828-30828/securitymsg.pdfjsviewer I/libpersona: KNOX_SDCARD not a persona
08-17 13:26:53.441 30828-30828/securitymsg.pdfjsviewer I/SELinux: Function: selinux_compare_spd_ram , priority [1] , priority version is VE=SEPF_GT-I9500_5.0.1_0051
08-17 13:26:53.441 30828-30828/securitymsg.pdfjsviewer E/SELinux: [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL
08-17 13:26:53.441 30828-30828/securitymsg.pdfjsviewer I/art: Late-enabling -Xcheck:jni
08-17 13:26:53.576 30828-30828/securitymsg.pdfjsviewer D/ResourcesManager: creating new AssetManager and set to /data/app/securitymsg.pdfjsviewer-1/base.apk
08-17 13:26:53.776 30828-30828/securitymsg.pdfjsviewer I/WebViewFactory: Loading com.google.android.webview version 37 (1589900-arm) (code 110601)
08-17 13:26:53.776 30828-30828/securitymsg.pdfjsviewer D/ResourcesManager: creating new AssetManager and set to /system/app/WebViewGoogle/WebViewGoogle.apk
08-17 13:26:53.796 30828-30828/securitymsg.pdfjsviewer I/LibraryLoader: Loading: webviewchromium
08-17 13:26:53.801 30828-30828/securitymsg.pdfjsviewer I/LibraryLoader: Time to load native libraries: 4 ms (timestamps 7436-7440)
08-17 13:26:53.801 30828-30828/securitymsg.pdfjsviewer I/LibraryLoader: Expected native library version number "",actual native library version number ""
08-17 13:26:53.861 30828-30828/securitymsg.pdfjsviewer V/WebViewChromiumFactoryProvider: Binding Chromium to main looper Looper (main, tid 1) {15ed6d55}
08-17 13:26:53.876 30828-30828/securitymsg.pdfjsviewer I/LibraryLoader: Expected native library version number "",actual native library version number ""
08-17 13:26:53.876 30828-30828/securitymsg.pdfjsviewer I/chromium: [INFO:library_loader_hooks.cc(106)] Chromium logging enabled: level = 0, default verbosity = 0
08-17 13:26:53.896 30828-30828/securitymsg.pdfjsviewer I/BrowserStartupController: Initializing chromium process, renderers=0
08-17 13:26:53.901 30828-30828/securitymsg.pdfjsviewer W/art: Attempt to remove local handle scope entry from IRT, ignoring
08-17 13:26:53.956 30828-30828/securitymsg.pdfjsviewer W/chromium: [WARNING:resource_bundle.cc(315)] locale_file_path.empty()
08-17 13:26:53.956 30828-30888/securitymsg.pdfjsviewer W/AudioManagerAndroid: Requires BLUETOOTH permission
08-17 13:26:53.956 30828-30828/securitymsg.pdfjsviewer I/chromium: [INFO:aw_browser_main_parts.cc(63)] Load from apk succesful, fd=39 off=46764 len=2953
08-17 13:26:53.956 30828-30828/securitymsg.pdfjsviewer I/chromium: [INFO:aw_browser_main_parts.cc(78)] Loading webviewchromium.pak from, fd:40 off:229520 len:643667
08-17 13:26:54.151 30828-30894/securitymsg.pdfjsviewer W/chromium: [WARNING:proxy_service.cc(901)] PAC support disabled because there is no system implementation
08-17 13:26:54.176 30828-30828/securitymsg.pdfjsviewer W/chromium: [WARNING:data_reduction_proxy_settings.cc(403)] SPDY proxy OFF at startup
08-17 13:26:54.216 30828-30828/securitymsg.pdfjsviewer W/art: Attempt to remove local handle scope entry from IRT, ignoring
08-17 13:26:54.226 30828-30828/securitymsg.pdfjsviewer W/AwContents: onDetachedFromWindow called when already detached. Ignoring
08-17 13:26:54.281 30828-30828/securitymsg.pdfjsviewer D/Activity: performCreate Call secproduct feature valuefalse
08-17 13:26:54.281 30828-30828/securitymsg.pdfjsviewer D/Activity: performCreate Call debug elastic valuetrue
08-17 13:26:54.311 30828-30948/securitymsg.pdfjsviewer D/OpenGLRenderer: Render dirty regions requested: true
08-17 13:26:54.326 30828-30828/securitymsg.pdfjsviewer D/Atlas: Validating map...
08-17 13:26:54.406 30828-30948/securitymsg.pdfjsviewer I/OpenGLRenderer: Initialized EGL, version 1.4
08-17 13:26:54.406 30828-30948/securitymsg.pdfjsviewer W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
08-17 13:26:54.426 30828-30948/securitymsg.pdfjsviewer D/OpenGLRenderer: Get maximum texture size. GL_MAX_TEXTURE_SIZE is 4096
08-17 13:26:54.426 30828-30948/securitymsg.pdfjsviewer D/OpenGLRenderer: Enabling debug mode 0
08-17 13:26:54.646 30828-30948/securitymsg.pdfjsviewer V/RenderScript: 0x9dd97600 Launching thread(s), CPUs 4
08-17 13:26:54.796 30828-30828/securitymsg.pdfjsviewer I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@35d335c2 time:105548437
08-17 13:26:55.321 30828-30975/securitymsg.pdfjsviewer I/chromium: [INFO:SkUtilsArm.cpp(179)] Device supports ARM NEON instructions!
08-17 13:27:21.821 30828-30834/securitymsg.pdfjsviewer I/art: Thread[2,tid=30834,WaitingInMainSignalCatcherLoop,Thread*=0xaf40e400,peer=0x12c79080,"Signal Catcher"]: reacting to signal 3
08-17 13:27:23.001 30828-30834/securitymsg.pdfjsviewer I/art: Wrote stack traces to '/data/anr/traces.txt'