pwlin / cordova-plugin-file-opener2

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

Failed to find configured root that contains "/data/data/{app_name_redacted}/files/" #282

Closed krishnagopinath closed 4 years ago

krishnagopinath commented 4 years ago

Expected Behaviour

PDF file must open in a PDF reader app that's installed in the Android phone.

Actual Behaviour

An error is thrown when cordova.plugins.fileOpener2.open is invoked with the PDF path.

java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/{app_name_redacted}/files/{namespace_redacted}/proposalOption/66c22598-400e-48cc-9bc4-a4f0abcc9713/01298545-00f1-45e0-8816-8bc7908c94c9
    at androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:739)
    at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:418)
    at io.github.pwlin.cordova.plugins.fileopener2.FileOpener2._open(FileOpener2.java:127)
    at io.github.pwlin.cordova.plugins.fileopener2.FileOpener2.execute(FileOpener2.java:68)

This is code snippet I use to open the file:

var namespace = 'namespace_redacted';
var filePath = 'proposalOption/66c22598-400e-48cc-9bc4-a4f0abcc9713/01298545-00f1-45e0-8816-8bc7908c94c9';
var nativeUrl = `${cordova.file.dataDirectory}/${namespace}/${filePath}`;
// nativeUrl => "file:///data/user/0/{app_name_redacted}/files/{namespace_redacted}/proposalOption/66c22598-400e-48cc-9bc4-a4f0abcc9713/01298545-00f1-45e0-8816-8bc7908c94c9"

window.resolveLocalFileSystemURL(nativeUrl, function(entry) {
    var cdvUrl = entry.toInternalURL();
    // cdvUrl => "cdvfile://localhost/files/{namespace_redacted}/proposalOption/66c22598-400e-48cc-9bc4-a4f0abcc9713/01298545-00f1-45e0-8816-8bc7908c94c9"

    cordova.plugins.fileOpener2.open(cdvUrl, "application/pdf", {
       error: (e) => console.error('Could not open file -- ', e)
    });
});

The file definitely exists:

Screen Shot 2019-12-24 at 12 25 45 PM

The manifest file (platforms/android/app/src/main/AndroidManifest.xml) contains the following snippet as expected:

<application android:hardwareAccelerated="true" ....>
    <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>
</application>

I checked existed issues #225 , #195 and #98 . None of the solutions listed worked for me 😢

Platform and Version (eg. Android 5.0 or iOS 9.2.1)

Android 10

(Android) What device vendor (e.g. Samsung, HTC, Sony...)

Google Pixel 2

Cordova CLI info

Node version: v12.13.0

Cordova version: 8.1.1

Config.xml file:

<?xml version='1.0' encoding='utf-8'?>
<widget id="{app_name_redacted}" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0" xmlns:gap="http://phonegap.com/ns/1.0">
    <!-- App info -->
    <content src="index.html" />
    <preference name="WKPort" value="*****" />

    <!-- Other prefs -->
    <preference name="iosPersistentFileLocation" value="Library" />
    <preference name="AndroidPersistentFileLocation" value="Internal" />
    <preference name="iosExtraFilesystems" value="library,library-nosync,documents,documents-nosync,cache,bundle,root" />
    <preference name="AndroidExtraFilesystems" value="files,files-external,documents,sdcard,cache,cache-external,root" />

    <platform name="android">
        <preference name="android-minSdkVersion" value="19" />
        <preference name="android-targetSdkVersion" value="28" />
        <preference name="AndroidLaunchMode" value="singleTask" />
        <!-- Other android settings -->
    </platform>
    <platform name="ios">
        <!-- iOS settings -->
    </platform>
    <engine name="android" spec="8.1.0" />
    <engine name="ios" spec="5.0.1" />
</widget>

Plugins:

code-push,cordova-android-play-services-gradle-release,cordova-android-support-gradle-release,cordova-plugin-androidx,cordova-plugin-androidx-adapter,cordova-plugin-camera,cordova-plugin-code-push,cordova-plugin-customurlscheme,cordova-plugin-device,cordova-plugin-dialogs,cordova-plugin-file,cordova-plugin-file-opener2,cordova-plugin-file-transfer,cordova-plugin-geolocation,cordova-plugin-inappbrowser,cordova-plugin-ionic-keyboard,cordova-plugin-ionic-webview,cordova-plugin-network-information,cordova-plugin-printer,cordova-plugin-splashscreen,cordova-plugin-statusbar,cordova-plugin-whitelist,cordova-plugin-zip

Android platform:

Available Android targets:==============] 100% Fetch remote repository...
id: 1 or "android-14"
     Name: Android API 14
     Type: Platform
     API level: 14
     Revision: 4
id: 2 or "android-16"
     Name: Android API 16
     Type: Platform
     API level: 16
     Revision: 5
id: 3 or "android-18"
     Name: Android API 18
     Type: Platform
     API level: 18
     Revision: 3
id: 4 or "android-19"
     Name: Android API 19
     Type: Platform
     API level: 19
     Revision: 4
id: 5 or "android-21"
     Name: Android API 21
     Type: Platform
     API level: 21
     Revision: 2
id: 6 or "android-22"
     Name: Android API 22
     Type: Platform
     API level: 22
     Revision: 2
id: 7 or "android-23"
     Name: Android API 23
     Type: Platform
     API level: 23
     Revision: 3
id: 8 or "android-24"
     Name: Android API 24
     Type: Platform
     API level: 24
     Revision: 2
id: 9 or "android-25"
     Name: Android API 25
     Type: Platform
     API level: 25
     Revision: 3
id: 10 or "android-26"
     Name: Android API 26
     Type: Platform
     API level: 26
     Revision: 2
id: 11 or "android-27"
     Name: Android API 27
     Type: Platform
     API level: 27
     Revision: 3
id: 12 or "android-28"
     Name: Android API 28
     Type: Platform
     API level: 28
     Revision: 6
id: 13 or "android-29"
     Name: Android API 29
     Type: Platform
     API level: 29
     Revision: 3

Plugin version

I installed the plugin via github, because npm did not have v3.0.0

cordova plugin add https://github.com/pwlin/cordova-plugin-file-opener2.git#45f5889
cordova-plugin-file-opener2 3.0.0 "File Opener2"

Other Relevant plugins that I have installed:

cordova-plugin-androidx 1.0.2 "cordova-plugin-androidx"
cordova-plugin-androidx-adapter 1.1.0 "cordova-plugin-androidx-adapter"
...
cordova-plugin-file-opener2 3.0.0 "File Opener2"
cordova-plugin-ionic-keyboard 2.2.0 "cordova-plugin-ionic-keyboard"
cordova-plugin-ionic-webview 2.5.3 "cordova-plugin-ionic-webview"
...
jefflnorton commented 4 years ago

Running into same issue on Android 10 Samsung Galaxy S10e.

pwlin commented 4 years ago

@krishnagopinath @jefflnorton Did you find a solution for this? Maybe you can test whether it still happens with the latest 3.0.1 version

bjdose commented 4 years ago

I have the same issue using 3.0.1 version. Any ideas?

pwlin commented 4 years ago

Have you tested to see you can read from cordova.file.externalDataDirectory? It might be that you do not have required rights to read from cordova.file.dataDirectory

ryaa commented 4 years ago

I have the same issue using 3.0.1 version. Any ideas?

Do you have cordova-camera-plugin v4.2.0 installed? There might be a problem with this and camera plugin conflicting - see https://github.com/apache/cordova-plugin-camera/issues/597#issuecomment-627968308

galfert commented 4 years ago

I'm seeing the same issue on Android 9, using version 3.0.1 of the plugin.

I did have the cordova-camera-plugin installed, but removed it and still have the same problem.

Also tested with both cordova.file.externalDataDirectory and cordova.file.dataDirectory.

mohammadzbggo commented 4 years ago

@galfert Did externalDataDirectory work for you? When I tested this via the emulator it worked fine. I'm still experiencing issues with dataDirectory.

The cordova-camera-plugin is installed in my case and is version 4.1.0 and it works fine when opening/saving images.

I looked into the Java source and xml for file paths and compared them with the solutions on stackoverflow. It looks ok but dunno why it's throwing the error.

Edit: I was able to fix it via removing the camera plugin reference in the manifest.xml. The dataDirectory started to work. Going to use @ryaa's fork of the camera plugin. Looks like a provider conflict of some kind.

AlexMiniApps commented 4 years ago

@galfert @pwlin I have researched the issue and find out that it is arises due to the cordova-plugin-camera have the same authority in the manifest file. It is needs just to update authority in the FileOpener2.java file and in the manifest.

  1. In the FileOpener2.java change all the strings from: https://github.com/pwlin/cordova-plugin-file-opener2/blob/master/src/android/io/github/pwlin/cordova/plugins/fileopener2/FileOpener2.java#L119 https://github.com/pwlin/cordova-plugin-file-opener2/blob/master/src/android/io/github/pwlin/cordova/plugins/fileopener2/FileOpener2.java#L127

...FileProvider.getUriForFile(context, cordova.getActivity().getPackageName() + ".provider", file); to ... FileProvider.getUriForFile(context, cordova.getActivity().getPackageName() + ".fileOpener2.provider", file);

  1. and in the AndroidManifest.xml from: <provider android:authorities="${applicationId}.provider" android:exported="false" android:grantUriPermissions="true" android:name="io.github.pwlin.cordova.plugins.fileopener2.FileProvider"> to <provider android:authorities="${applicationId}.fileOpener2.provider" android:exported="false" android:grantUriPermissions="true" android:name="io.github.pwlin.cordova.plugins.fileopener2.FileProvider">

@pwlin I can create PR if needed

pwlin commented 4 years ago

@AlexMiniApps Thanks for your findings. If you send a PR, I'll merge it right away.

AlexMiniApps commented 4 years ago

@pwlin please review the PR https://github.com/pwlin/cordova-plugin-file-opener2/pull/302

pwlin commented 4 years ago

@AlexMiniApps I have merged your PR and pushed a new version 3.0.3 to NPM. Can you please test it works?

AlexMiniApps commented 4 years ago

@pwlin I have checked. It works. Thanks!

galfert commented 4 years ago

I can confirm it to be working as well now. Thanks a lot!