Exception when using Gluon-mobile ShareService to open PDF from application on android

风格不统一 提交于 2021-01-29 21:35:43

问题


In my application I'd like to show a help file (PDF) to the user. The help file is published with the resources and on first request copied to the public storage. This works! I can see the file and open it with Adobe Acrobat on the device.

But on invoking the Gluon-mobile ShareService I get exception: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference

I use ShareService 3.8.6 on Android 9.1.

Here is the code:

private void onHelp(ActionEvent event)
{
    Optional<StorageService> oss = Services.get(StorageService.class);
    if(oss.isPresent())
    {
        StorageService ss = oss.get();
        Optional<File> of = ss.getPublicStorage("");
        if(of.isPresent())
        {
            File parentfolder = of.get();
            if(parentfolder != null && parentfolder.exists() && parentfolder.canWrite())
            {
                File publicPdf = new File(parentfolder.getAbsolutePath(), "helpfile.pdf");
                if(!publicPdf.exists())
                {
                    InputStream origPdf = getClass().getClassLoader().getResourceAsStream("files/helpfile.pdf");
                    try {
                        FileOutputStream fos = new FileOutputStream(publicPdf);
                        while (origPdf.available() > 0) {
                              fos.write(origPdf.read());
                        }
                        fos.close();
                        origPdf.close();
                    } catch (IOException e) {
                        System.out.println(e.getMessage());
                    }
                }
                try {
                    //EDIT:
                    System.out.println(publicPdf.getAbsolutePath() + " exists: "
                            + publicPdf.exists()+ " canRead: "+ publicPdf.canRead());
                    Services.get(ShareService.class).ifPresent(service -> {
                        service.share("application/pdf", publicPdf);
                    });                 
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                }
            }
        }
    }
}

EDIT: I added System.out.println just before the ShareService and adb logcat shows

03-02 17:30:24.119 20119 20146 I System.out: /storage/emulated/0/helpfile.pdf exists: true canRead: true

EDIT2:

logcat output with System.setProperty("com.gluonhq.charm.down.debug","true") set plus e.printStackTrace() in catch(Exception e) after service.share(...).

I System.out: /storage/emulated/0/helpfile.pdf exists: true canRead: true
I AndroidShareService: File to share: /storage/emulated/0/helpfile.pdf
I AndroidShareService: Application name provider: com.myapp.ui.javafx.fileprovider
W myapp.ui.javaf: Class android.support.v4.content.FileProvider failed lock verification and will run slower.
W System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
W System.err:   at android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:584)
W System.err:   at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:558)
W System.err:   at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:400)
W System.err:   at com.gluonhq.charm.down.plugins.android.AndroidShareService.share(AndroidShareService.java:105)
W System.err:   at com.gluonhq.charm.down.plugins.android.AndroidShareService.share(AndroidShareService.java:82)
W System.err:   at com.myapp.ui.javafx.GMMain.lambda$onHelp$1(GMMain.java:280)
W System.err:   at com.myapp.ui.javafx.GMMain$$Lambda$3.accept(Unknown Source:8)
W System.err:   at java.util.Optional.ifPresent(Optional.java:155)
W System.err:   at com.myapp.ui.javafx.GMMain.onHelp(GMMain.java:279)
...

logcat output when accessing private storage Optional<File> of = ss.getPrivateStorage();:

I System.out: /data/user/0/com.myapp.ui.javafx/files/helpfile.pdf exists: true canRead: true
I AndroidShareService: File to share: /data/user/0/com.myapp.ui.javafx/files/helpfile.pdf
I AndroidShareService: Application name provider: com.myapp.ui.javafx.fileprovider
W myapp.ui.javaf: Class android.support.v4.content.FileProvider failed lock verification and will run slower.
W System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
W System.err:   at android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:584)
W System.err:   at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:558)
W System.err:   at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:400)
W System.err:   at com.gluonhq.charm.down.plugins.android.AndroidShareService.share(AndroidShareService.java:105)
W System.err:   at com.gluonhq.charm.down.plugins.android.AndroidShareService.share(AndroidShareService.java:82)
W System.err:   at com.myapp.ui.javafx.GMMain.lambda$onHelp$1(GMMain.java:280)
W System.err:   at com.myapp.ui.javafx.GMMain$$Lambda$3.accept(Unknown Source:8)
W System.err:   at java.util.Optional.ifPresent(Optional.java:155)
W System.err:   at com.myapp.ui.javafx.GMMain.onHelp(GMMain.java:279)
...

The exception thrown looks pretty much the same for both cases.

来源:https://stackoverflow.com/questions/60489911/exception-when-using-gluon-mobile-shareservice-to-open-pdf-from-application-on-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!