cordova-plugin-ionic-webview icon indicating copy to clipboard operation
cordova-plugin-ionic-webview copied to clipboard

convertFileSrc producing output starting with 'undefined'

Open rlyttle opened this issue 5 years ago • 20 comments

Environment info: cordova-plugin-ionic-webview 3.1.2 ionic-angular 3.9.2 cordova-android 7.1.4

Running into a problem on Android where the following will intermittently produce either file URIs starting with "http://localhost" or "undefined"

let profileImage = this.win.Ionic.WebView.convertFileSrc( this.file.dataDirectory + profileImageFileName );
console.log("processing profileImage = " + profileImage);

Example output when it works: processing profileImage = http://localhost/_app_file_/data/user/0/com.company.app/files/724MjcgqPDur83ZAMyErlw==-image.jpg" Example output when it fails: processing profileImage = undefined/_app_file_/data/user/0/com.company.app/files/724MjcgqPDur83ZAMyErlw==-image.jpg"

This issue seems to happen intermittently on Android platform only. Possibly related to https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/193 The workaround in #193 does not solve my issue.

Any suggestions?

rlyttle avatar May 09 '19 10:05 rlyttle

I can't reproduce, can you provide some sample project or steps to reproduce?

jcesarmobile avatar Jun 05 '19 11:06 jcesarmobile

I can't reproduce, can you provide some sample project or steps to reproduce?

What does it return for you? I want to see an example of the url that this function has to return on android platform given this: this.webView.convertFileSrc('file:///Users/dan/camera-image-12345.png'). I also get undefined/app_file/Users/...

mazolo avatar Jun 07 '19 09:06 mazolo

It returns the configured scheme :// the configured hostname, with default values http://localhost

jcesarmobile avatar Jun 07 '19 09:06 jcesarmobile

It returns the configured scheme :// the configured hostname, with default values http://localhost

If possible, I would like to see the exact output given the invocation I supplied on android device. It might be I'm running latest version of ionic. But it just returns undefined.

mazolo avatar Jun 07 '19 09:06 mazolo

If possible, I would like a sample app reproducing the issue.

For the url you provided it should return

http://localhost/app_file/Users/dan/camera-image-12345.png

jcesarmobile avatar Jun 07 '19 09:06 jcesarmobile

If possible, I would like a sample app reproducing the issue.

For the url you provided it should return

http://localhost/app_file/Users/dan/camera-image-12345.png

Thanks for reply, the port number will not be included?

mazolo avatar Jun 07 '19 09:06 mazolo

It depends on the plugin version, 3.x and newer don’t have port, unless you put it as part of the hostname preference

jcesarmobile avatar Jun 07 '19 09:06 jcesarmobile

It depends on the plugin version, 3.x and newer don’t have port, unless you put it as part of the hostname preference

Whenever you have chance please take a look at my settings: ionic info:

Ionic:

Ionic CLI : 5.0.0 (...\AppData\Roaming\npm\node_modules\ionic) Ionic Framework : @ionic/angular 4.4.2 @angular-devkit/build-angular : 0.13.9 @angular-devkit/schematics : 7.3.9 @angular/cli : 7.3.9 @ionic/angular-toolkit : 1.5.1

Cordova:

Cordova CLI : 9.0.0 ([email protected]) Cordova Platforms : android 8.0.0 Cordova Plugins : cordova-plugin-ionic-keyboard 2.1.3, cordova-plugin-ionic-webview 4.0.1, (and 5 other plugins)

Utility:

cordova-res : 0.3.0 native-run : 0.2.2

System:

NodeJS : v10.16.0 (C:\Program Files\nodejs\node.exe) npm : 6.9.0 OS : Windows 10

mazolo avatar Jun 07 '19 09:06 mazolo

window.WEBVIEW_SERVER_URL is the culprit that returns undefined. How can I go around this given my configuration?

mazolo avatar Jun 07 '19 12:06 mazolo

I fail to believe that this was ever tested:

import { WebView } from '@ionic-native/ionic-webview/ngx';


constructor(private webview: WebView) { }

...

img = this.webview.convertFileSrc('file:///Users/dan/camera-image-12345.png')

mazolo avatar Jun 07 '19 12:06 mazolo

window.WEBVIEW_SERVER_URL is generated by the webview plugin.

I've tried once again and it's working fine. So, please, provide a sample project or I'll close the issue.

jcesarmobile avatar Jun 07 '19 13:06 jcesarmobile

var str = this.webview.convertFileSrc(this.file.externalDataDirectory+"fondo.png"); var re = /undefined/gi; str = str.replace(re, "http://localhost");

temporary solution

magallo286 avatar Jun 20 '19 02:06 magallo286

Are there any news on this? I can't get convertFileSrc to work at all - always returns undefined:///

I cloned this repo: https://github.com/ionic-team/webinar-capflow because I saw this is using the convertFileSrc function and wanted to see if this one works - well, it doesn't.

The images aren't showing up and when I console.log(this.webview.convertFileSrc(img)), it also returns undefined.

Even adding and doesn't make a difference. Always getting "undefined:///" as return value.

Plz Help :(

therawk avatar Nov 11 '19 16:11 therawk

I've tested webinar-capflow app and convertFileSrc is working fine. Can you provide more information about how are you running the app or some other details that might help to reproduce the issue?

jcesarmobile avatar Nov 12 '19 20:11 jcesarmobile

Thanks for your reply! I just realized this error only occured when I'm using the --devapp option. This was bugging me as hell so I deployed it to my actual device and it worked 👍

It would be great if you could try this on the devapp and let me know if it works for you. When using an emulator (Android Studio) everything works fine....

therawk avatar Nov 13 '19 09:11 therawk

in that case it's probably a bug in devApp, not in the plugin. Are you testing on Android or iOS? iOS app has not been updated in almost 2 years, so probably has a very old version of the plugin which might not have convertFileSrc implemented

jcesarmobile avatar Nov 13 '19 12:11 jcesarmobile

I solved my problem by adding <allow-navigation href="file://*/*" /> in config.xml

andrelicoski avatar Dec 05 '19 20:12 andrelicoski

I am running in this exact issue -- files like file:///data/user/0/io.ionic.devapp/files/1578114145719.jpg gets transformed into undefined:///data/user/0/io.ionic.devapp/files/1578114145719.jpg. That's on android, with the latest Ionic devapp (updated feb 6 2019). On IOS, I get:

[ng] [console.warn]: "Native: tried calling WebView.convertFileSrc, but the WebView plugin is not installed."
[ng] [console.warn]: "Install the WebView plugin: 'ionic cordova plugin add cordova-plugin-ionic-webview'"

Also, on android, manually rewriting the url with something like file.replace(/^file:\/\/\//, 'http://localhost/_app_file_/') does not work. I'm new to ionic dev and I'm beginning to suspect that the devapp is garbage.

@andrelicoski Can you elaborate exactly where you put that line, and what exactly it fixed, on what platform?

tnishimura avatar Jan 04 '20 05:01 tnishimura

We have this problem as well in live reload mode.

Turns out it was because of the CDVWebview option that has been overridden in config.xml. That's why we got window.WEBVIEW_SERVER_URL undefined.

thesayyn avatar Jan 17 '20 12:01 thesayyn

Just had the same problem on iOS after ionic-webview suddenly stopped working, so I uninstalled and reinstalled the plugin:

cordova plugin remove cordova-plugin-ionic-webview cordova plugin add cordova-plugin-ionic-webview

window.Ionic.WebView.convertFileSrc started working like a charm again.

B0CHI avatar Sep 04 '20 14:09 B0CHI