Skip to content

openFile hangs #4892

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jokro opened this issue Jul 22, 2019 · 4 comments
Closed

openFile hangs #4892

jokro opened this issue Jul 22, 2019 · 4 comments

Comments

@jokro
Copy link

jokro commented Jul 22, 2019

After upgrading to latest version of nativescript and node openFile hangs with the following log:

Error: Error in openFile: 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 androidx.core.content.FileProvider.parsePathStrategy(FileProvider.java:605) androidx.core.content.FileProvider.getPathStrategy(FileProvider.java:579) androidx.core.content.FileProvider.getUriForFile(FileProvider.java:417) com.tns.Runtime.callJSMethodNative(Native Method) com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1242) com.tns.Runtime.callJSMethodImpl(Runtime.java:1122) com.tns.Runtime.callJSMethod(Runtime.java:1109) com.tns.Runtime.callJSMethod(Runtime.java:1089) com.tns.Runtime.callJSMethod(Runtime.java:1081) com.tns.gen.java.lang.Object_vendor_14095_32_ClickListenerImpl.onClick(Object_vendor_14095_32_ClickListenerImpl.java:18) android.view.View.performClick(View.java:6600) android.view.View.performClickInternal(View.java:6577) android.view.View.access$3100(View.java:781) android.view.View$PerformClick.run(View.java:25912) android.os.Handler.handleCallback(Handler.java:873) android.os.Handler.dispatchMessage(Handler.java:99) android.os.Looper.loop(Looper.java:193) android.app.ActivityThread.main(ActivityThread.java:6923) java.lang.reflect.Method.invoke(Native Method) com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) com.android.internal.os.ZygoteInit.main(ZygoteInit.java:870) [file:///data/data/org.nativescript.tester/files/app/vendor.js] Error: Error in openFile: 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 androidx.core.content.FileProvider.parsePathStrategy(FileProvider.java:605) androidx.core.content.FileProvider.getPathStrategy(FileProvider.java:579) androidx.core.content.FileProvider.getUriForFile(FileProvider.java:417) com.tns.Runtime.callJSMethodNative(Native Method) com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1242) com.tns.Runtime.callJSMethodImpl(Runtime.java:1122) com.tns.Runtime.callJSMethod(Runtime.java:1109) com.tns.Runtime.callJSMethod(Runtime.java:1089) com.tns.Runtime.callJSMethod(Runtime.java:1081) com.tns.gen.java.lang.Object_vendor_14095_32_ClickListenerImpl.onClick(Object_vendor_14095_32_ClickListenerImpl.java:18) android.view.View.performClick(View.java:6600) android.view.View.performClickInternal(View.java:6577) android.view.View.access$3100(View.java:781) android.view.View$PerformClick.run(View.java:25912) android.os.Handler.handleCallback(Handler.java:873) android.os.Handler.dispatchMessage(Handler.java:99) android.os.Looper.loop(Looper.java:193) android.app.ActivityThread.main(ActivityThread.java:6923) java.lang.reflect.Method.invoke(Native Method) com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) com.android.internal.os.ZygoteInit.main(ZygoteInit.java:870)

I tested with a newly created project and test program for android:
TS:
`import { EventData } from "tns-core-modules/data/observable";
import { Page } from "tns-core-modules/ui/page";
import {FileSystemAccess} from "tns-core-modules/file-system/file-system-access"
const fsa=new FileSystemAccess()

import { openFile} from "tns-core-modules/utils/utils"

class ViewModel {
onTest(){
let path="/storage/emulated/0/Download/test.txt"
fsa.writeText(path,"tester")
console.log(fsa.fileExists(path))
openFile(path)
}
}

export function navigatingTo(args: EventData) {
const page = args.object;
page.bindingContext = new ViewModel()
}
`

XML:
<Page navigatingTo="navigatingTo"> <Button text="Test" tap="{{onTest}}"/> </Page>

tns doctor shows:

√ Your ANDROID_HOME environment variable is set and points to correct directory. √ Your adb from the Android SDK is correctly installed. √ The Android SDK is installed. √ A compatible Android SDK for compilation is found. √ Javac is installed and is configured properly. √ The Java Development Kit (JDK) is installed and is configured properly. √ Local builds for iOS can be executed only on a macOS system. To build for iOS on a different operating system, you can use the NativeScript cloud infrastructure. √ Getting NativeScript components versions information... √ Component nativescript has 6.0.1 version and is up to date. √ Component tns-core-modules has 6.0.1 version and is up to date. √ Component tns-android has 6.0.0 version and is up to date. √ Component tns-ios has 6.0.1 version and is up to date.

@DanLatimer
Copy link

DanLatimer commented Jul 23, 2019

I was having this problem too. Make sure you have a file provider defined in your AndroidManifest.xml that helped me:

        <provider android:name="androidx.core.content.FileProvider" android:authorities="<YOUR PACKAGE NAME GOES HERE>.provider" android:exported="false" android:grantUriPermissions="true">
            <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths"/>
        </provider>

^^ that is the androidx version (nativescript 6+) if you are using nativescript < 6 use something like this

        <provider android:name="android.support.v4.content.FileProvider" android:authorities="<YOUR PACKAGE NAME GOES HERE>.provider" android:exported="false" android:grantUriPermissions="true">
            <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths"/>
        </provider>

don't forget to fill in the <YOUR PACKAGE NAME GOES HERE>

@jokro
Copy link
Author

jokro commented Jul 24, 2019

@DanLatimer, thanks for the hint.

My manifest file starts with:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
	package="__PACKAGE__"

Hence I used "PACKAGE" as the (placeholder) for the package name.
In the manifest file entered your code inside tags application:

<application...>
...
<provider android:name="androidx.core.content.FileProvider" android:authorities="__PACKAGE__.provider" android:exported="false" android:grantUriPermissions="true">
            <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths"/>
        </provider>
        </provider>

</application>

When building I get an error that xml/provider_paths.xml is missing., I added this file Android/main/res/xml/provider_paths.xml with the following content:

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
    <files-path name="files" path="."/>
</paths>

Now it worked. I hope this can help other people.

I suggest your and mine additions are added to the plugin ?!
Thx for your reaction.

@Fatme
Copy link
Contributor

Fatme commented Jul 30, 2019

@jokro, @DanLatimer

Thank you for sharing this!

I'm closing this thread, you can log a separate issue in tns-core-modules and discuss if your additions can be added.

@davorpeic
Copy link

Also, if you are saving files to temp() folder (cache folder) you need to also whitelist it

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
    <files-path name="files" path="."/>
    <cache-path name="cache" path="." />
</paths>

dp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants