0

I tried the following code which uses PDF.js (check this answer for reference) for opening a PDF file on android but despite the fact that it does work fine on Oneplus3 with Android N and Marshmallow devices but it fails to render with a hang issue on certain samsung devices (I tested on Samsung Galaxy S4) with Lollypop or some other devices with ICS or Jelly Bean (Tested with Genymotion). The hang issue is so bad that I do not see any logs from device all I can to get rid of hang issue is to remove battery and turn the device back on. This issue also occurs with MuPDF PDF viewer. I would like to know the solution to the problem.

Code :

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

}

Is this high memory consuming viewer or Javascript is the reason for the problem? Given the ideal small app size that the above method takes and the support if above issue is fixed it can give to older devices which PDFRenderer (introduced in later Android API versions and produces security exceptions) cannot, I find this method of viewing PDF files ideal.

Log Cat Output :

    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'
Abhishek Singh
  • 415
  • 1
  • 12
  • 24
  • You can try [other options for viewing the PDF file](https://commonsware.com/blog/2017/01/04/options-viewing-pdfs.html), or use `ACTION_VIEW` to have a PDF viewer app display it. – CommonsWare Aug 16 '17 at 17:10
  • @CommonsWare I want my own app to view pdf and do not wish to use other viewers. Android's native PDFRenderer introduced in latest APIs has been causing security exception crashes on various devices. – Abhishek Singh Aug 16 '17 at 17:12
  • "I want my own app to view pdf and do not wish to use other viewers" -- then you will need to limit your PDFs to ones that your own viewing technology can handle, across all devices. My blog post outlines two others (`PDF.js` and pdfium) that you are welcome to try. However, you may well still run into problems, at which point you will need to simplify this PDF to the point where it works reliably on your target devices when rendered with your chosen PDF rendering technology. – CommonsWare Aug 16 '17 at 17:18
  • @CommonsWare The above code which I have mentioned in the question is based on PDF.JS method. I will check pdfium. Thanks! – Abhishek Singh Aug 16 '17 at 17:21
  • @CommonsWare pdfium is too big library so I cannot use it. I need a tiny library to open PDF offline. – Abhishek Singh Aug 16 '17 at 18:28
  • Then you will need to simplify your PDF, until it works reliably. – CommonsWare Aug 16 '17 at 18:31
  • @CommonsWare What do you mean by simplify PDF? Can you support your comment with code or with a technical standpoint. Perhaps another answer on stackoverflow might help in case you want to refer it. – Abhishek Singh Aug 16 '17 at 19:09
  • "What do you mean by simplify PDF?" -- something in your PDF is causing rendering problems. You need to experiment, identify what that is, and stop using it. Continue this process until you get reliable rendering on all of your target devices. Or, switch to a different format (e.g., convert the PDF to HTML, or to PNG files). – CommonsWare Aug 16 '17 at 19:10

0 Answers0