capacitor-community / file-opener

Capacitor File Opener. The plugin is able to open a file given the mimeType and the file uri. This plugin is similar to cordova-plugin-file-opener2 without installation support.
MIT License
67 stars 13 forks source link

Error check permission #47

Closed faienz93 closed 1 year ago

faienz93 commented 1 year ago

Describe the bug First of all, thank you for your work. I decided to switch from @awesome-cordova-plugins/file-opener to @capacitor-community/file-opener because the first one is not more manteined. I have a problem/doubt on how to use or setup the plugin for proper use. The plugins seems to work smoothly but i get a strange error message in Android Studio:

java.lang.SecurityException: Permission Denial: writing com.ryltsov.alex.plugins.file.opener.FileOpenerProvider uri content://XXXX/files/XXXX.pdf from pid=18378, uid=10078 requires the provider be exported, or grantUriPermission()
at android.content.ContentProvider.enforceWritePermissionInner(ContentProvider.java:1025)
at android.content.ContentProvider.semEnforceWritePermission(ContentProvider.java:951)
at android.content.ContentProvider$Transport.enforceWritePermission(ContentProvider.java:730)
at android.content.ContentProvider$Transport.enforceFilePermission(ContentProvider.java:697)
at android.content.ContentProvider$Transport.openAssetFile(ContentProvider.java:506)
at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:272)
at android.os.Binder.execTransactInternal(Binder.java:1316)
at android.os.Binder.execTransact(Binder.java:1280)
Writing exception to parcel
java.lang.SecurityException: Permission Denial: writing com.ryltsov.alex.plugins.file.opener.FileOpenerProvider uri content://XXXX/files/XXXXX.pdf from pid=18378, uid=10078 requires the provider be exported, or grantUriPermission()
    at android.content.ContentProvider.enforceWritePermissionInner(ContentProvider.java:1025)
    at android.content.ContentProvider.semEnforceWritePermission(ContentProvider.java:951)
    at android.content.ContentProvider$Transport.enforceWritePermission(ContentProvider.java:730)
    at android.content.ContentProvider$Transport.enforceFilePermission(ContentProvider.java:697)
    at android.content.ContentProvider$Transport.openAssetFile(ContentProvider.java:506)
    at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:272)
    at android.os.Binder.execTransactInternal(Binder.java:1316)
    at android.os.Binder.execTransact(Binder.java:1280)

Despide this error, my device is able to open the PDF, but I am afraid if other device cannot.

I have added, as described in the readme the permission even if I write on Data directory and not external storage, not more allowed

+ <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

My Implementation

const openFile = async (path: string, contentType: string = 'application/pdf', openWithDefault: boolean = true) => {

    if (Capacitor.isNativePlatform()) {
        await FileOpener.open({
            filePath: path,
            contentType: contentType,
            openWithDefault: openWithDefault
          })

    }  else {
        console.log("Questa funzione è supportata solo dai dispositivi nativi, non dal Web");
    }      
}

and then use with capacitor/filesystem plugin

export const saveAndOpenPdf = (filename: string, pdf: string) => {
    writeFile(filename, pdf).then(fileWrited => {
      let path = fileWrited.uri
      let contentType = 'application/pdf'
      let openWithDefault = true;
      openFile(path, contentType, openWithDefault)
        .then(()=> { 
          console.log("File Opened!")
        })
        .catch(()=> {
          console.log("`Error!!`");
        });
    });
}

Am I making any mistakes?

Smartphone:

Additional context

Ionic:

   Ionic CLI                     : 6.20.1 (C:\ProgramData\nvm\v16.18.1\node_modules\@ionic\cli)
   Ionic Framework               : @ionic/angular 5.9.4
   @angular-devkit/build-angular : 12.2.18
   @angular-devkit/schematics    : 12.1.4
   @angular/cli                  : 12.1.4
   @ionic/angular-toolkit        : 6.1.0

Capacitor:

   Capacitor CLI      : 5.4.0
   @capacitor/android : 5.4.0
   @capacitor/core    : 5.4.0
   @capacitor/ios     : not installed
ryaa commented 1 year ago

I think, as the error suggests, that the problem is related to the permission and not to the plugin itself. For more details please see https://developer.android.com/training/data-storage#scoped-storage Let me know if this helps to resolve the problem. If not, can you please provide a repository in github demonstrating the problem

faienz93 commented 1 year ago

Hello, after some research and tests, I have noticed that the problem appears only if I use OneDrive to open the PDF. Using other applications such as Adobe Reader, Google Drive, Files by Google, Samsung Notes etc, the problem does not appear. To resolve the issue, based on this answer, I tried to add the following lines of code, but, unfortunally, without success:

intent.setDataAndType(path, contentType);
intent.setClipData(ClipData.newRawUri("", path));
ryaa commented 1 year ago

No repository to reproduce the issue has been provided. The problem is related to the permission and not to the plugin itself. Closing as the issue is invalid.