Saiful Zico
Saiful Zico

Reputation: 31

Why 'share PDF' and 'open PDF' not working on Android with Nativescript version 6.2

I have recently updated my nativescript 5.2 version to 6.2 and I found out that Share or Open PDF on my Android device is not possible and it is giving an error message whenever I try to share or save the PDF.

As part of migration I have changed android.support.v4.content.FileProvider to androidx.core.content.FileProvider.

Logs only show:

storage/emulated/0/Download/DocPDFs/risk_assessment_Lite_Version_201912122019.pdf JS: Android Share Error TypeError: Cannot read property 'startActivity' of undefined

    var parent = args.object.parent;

    if ( app.android ) {
        try {
            var shareFileList = new java.util.ArrayList();
            var file = new java.io.File( parent.path );
           // var fileUri = android.support.v4.content.FileProvider.getUriForFile(app.android.currentContext, appProvider, file);
           var fileUri = androidx.core.content.FileProvider.getUriForFile(app.android.currentContext, appProvider, file);

            shareFileList.add( fileUri );
            
            var shareIntent = new android.content.Intent();
            shareIntent.setAction(android.content.Intent.ACTION_SEND_MULTIPLE);
            shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody1 + userName + shareBody2 + parent.name + shareBody3);
            shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, shareSubject + parent.name);
            var emailList = new java.lang.reflect.Array.newInstance(java.lang.String.class, 1);
            emailList[0] = userEmail;
            shareIntent.putExtra(android.content.Intent.EXTRA_EMAIL, emailList);
            shareIntent.putParcelableArrayListExtra(android.content.Intent.EXTRA_STREAM, shareFileList);
            shareIntent.setType("*/*");
            shareIntent.addFlags(android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION);
            app.android.currentContext.startActivity(android.content.Intent.createChooser(shareIntent, _L("sharechecklisttoint")));
        } catch (error) {
            console.log("Android Share Error -> " + error);
            //new toastyModule.Toasty("Ein Fehler ist beim Teilen aufgetreten.").show();
            feedback.error({
                title: _L("error1"),
                backgroundColor: new color.Color("#434142"),
                message: "An error occurred while sharing."
            });
        }```

Does Anybody has an Idea?
Thanks 

Upvotes: 0

Views: 414

Answers (3)

Saiful Zico
Saiful Zico

Reputation: 31

app.android.foregroundActivity.startActivity(...) solved the problems :) Thanks @Manoj

Upvotes: 2

Manoj
Manoj

Reputation: 21908

currentContext is no longer valid, refer API spec for right list of attributes.

Use app.android.foregroundActivity.startActivity(...) or app.android.context.startActivity(...).

Upvotes: 2

Vyacheslav
Vyacheslav

Reputation: 1

Try too add this flag before calling shareIntent

shareIntent.addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK)

Upvotes: 0

Related Questions