pwlin / cordova-plugin-file-opener2

A File Opener Plugin for Cordova
MIT License
314 stars 584 forks source link

Error opening file Attempt to invoke virtual method #276

Closed CarolineSan closed 1 year ago

CarolineSan commented 4 years ago

Ionic version: 3.9.5 Angular 5

I am trying to open a PDF file in my Ionic application and get the following error:

Error opening file 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

So I followed the answers from this issue: https://github.com/pwlin/cordova-plugin-file-opener2/issues/215

I added this code to my AndroidManifest.xml:

<provider android:authorities="${applicationId}.opener.provider" android:exported="false" android:grantUriPermissions="true" android:name="io.github.pwlin.cordova.plugins.fileopener2.FileProvider">
          <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/opener_paths" tools:replace="android:authorities" />
 </provider>

And then I was getting the following error when I try to build:

Execution failed for task ':processDebugManifest'. Manifest merger failed with multiple errors

So I took the code from AndroidManifest and put it in my config.xml like this:

<edit-config file="AndroidManifest.xml" mode="merge" target="/manifest/application" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">
            <application android:networkSecurityConfig="@xml/network_security_config">
                <provider android:authorities="${applicationId}.opener.provider" android:exported="false" android:grantUriPermissions="true" android:name="io.github.pwlin.cordova.plugins.fileopener2.FileProvider">
                    <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/opener_paths" tools:replace="android:authorities" />
                </provider>
            </application>
        </edit-config>

And I managed to build it but it was no use and I still have the same error when trying to open a file.

This is my code:

writeFile(base64:any, contentType: any, fileName: any) { 

    var DataBlob = this.base64toBlob(base64, contentType);  

    var filePath = this.file.externalRootDirectory + "download";

    this.file.writeFile(filePath, fileName, DataBlob, contentType).then((success) => {  

        this.fileOpener.open(success.toURL(), this.contentType)
            .then(() => console.log('File is opened'))
            .catch(e => console.log('Error opening file', e));

    }).catch((err) => {  
        console.log("Error Occured While Writing File", err);
    });  
  }
pierresh commented 4 years ago

I tried to do these changes but none of them worked, I even could not build my app anymore. Same by undoing these changes...

So I end up to uninstall cordova android 8.1.0 and re-installed it, and it is working now ^^ Even no need to make any change in config.xml / AndroidManifest.xml.

I suppose the key is to install Fileopener2 before installing the platform.

CarolineSan commented 4 years ago

uninstall cordova android

You mean uninstall the Android Platform?

Is this it? ionic cordova rm platform android

pierresh commented 4 years ago

uninstall cordova android

You mean uninstall the Android Platform?

Is this it? ionic cordova rm platform android

Yes, exactly, and then cordova platform add android@8.1.0

CarolineSan commented 4 years ago

uninstall cordova android

You mean uninstall the Android Platform? Is this it? ionic cordova rm platform android

Yes, exactly, and then cordova platform add android@8.1.0

I did it and still got the same error :(

CarolineSan commented 4 years ago

@pierresh wich version of Ionic you were using when solved this issue?

pierresh commented 4 years ago

Hello,

Here is my ionic info

Ionic:

   ionic (Ionic CLI)  : 4.12.0
   Ionic Framework    : ionic-angular 3.9.8
   @ionic/app-scripts : 3.2.3

Cordova:

   cordova (Cordova CLI) : 8.1.2 (cordova-lib@8.1.1)
   Cordova Platforms     : android 8.1.0, browser 5.0.3, ios 4.5.5
   Cordova Plugins       : cordova-plugin-ionic-keyboard 2.1.2, cordova-plugin-ionic-webview 1.2.1, (and 24 other plugins)

System:

   ios-deploy : 1.9.2
   NodeJS     : v10.16.0 (/usr/local/bin/node)
   npm        : 6.9.0
   OS         : macOS Mojave
   Xcode      : Xcode 11.2.1 Build version 11B500

By the way, I did not make any change in config.xml / AndroidManifest.xml, I advise you to remove those you describe in your first message.

lihaitao123321 commented 4 years ago

我也遇到了这个问题,我是android@8.1.0,2.2.1有问题,我删除了2.2.1,添加2.2.0就可以了。cordova plugin rm cordova-plugin-file-opener2,cordova plugin add cordova-plugin-file-opener2@2.2.0。

etorres1193 commented 4 years ago

@CarolineSan add this to your config.xml

<platform name="android">
 ...
  <config-file parent="/manifest/application" target="AndroidManifest.xml">
            <provider android:authorities="${applicationId}.opener.provider" android:exported="false" android:grantUriPermissions="true" android:name="io.github.pwlin.cordova.plugins.fileopener2.FileProvider">
                <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/opener_paths" />
            </provider>
        </config-file>
...
</platform>

now it's working for me.. I'am not sure why the plugin it's not doing this anymore.

CarolineSan commented 4 years ago

@CarolineSan add this to your config.xml

<platform name="android">
 ...
  <config-file parent="/manifest/application" target="AndroidManifest.xml">
            <provider android:authorities="${applicationId}.opener.provider" android:exported="false" android:grantUriPermissions="true" android:name="io.github.pwlin.cordova.plugins.fileopener2.FileProvider">
                <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/opener_paths" />
            </provider>
        </config-file>
...
</platform>

now it's working for me.. I'am not sure why the plugin it's not doing this anymore.

Thanks!! It worked! I had to downgrade the version too

pwlin commented 4 years ago

The plugin should do this automatically and there is no need to add this manually.