简体   繁体   中英

How to share an image from external app to an Ionic 4 app

I'm trying to share an image using the "share with" function from another app to my Ionic 4 app. My app has to handle the image and use it. I've tried this solution but it is for Ionic 3. The plugins seems to be not mantained anymore. There has to be a solution for such a common task in Ionic 4.

I've tried the darryncampbell-cordova-plugin-intent without success. My code is

    initializeApp() {
            this.platform.ready().then(() => {
                (<any>window).plugins.intentShim.registerBroadcastReceiver({
                    filterActions: [
                        'com.darryncampbell.cordova.plugin.broadcastIntent.ACTION'
                    ],
                    filterCategories: [
                        'android.intent.category.DEFAULT',
                    ],
                }, (intent) => {
                    //your code to be executed after the broadcast is received..
                    console.log('Received Intent: ' + JSON.stringify(intent.extras));
                });
            //more code...

This code do nothing. Nothing at all. Iwas able to edit my config.xml to configure the manifest so my app shows up in the share with menus. This works well.

<intent-filter android:autoVerify="true">
    <action android:name="android.intent.action.SEND" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="image/*" />
</intent-filter>

I know this is late replay for OP, but I thought it will helpful someone..

I have used cordova-plugin-openwith plugin to achieve this. This will helps you to share an image using the "share with" function from another app to our Ionic 4 app.

You can Initialize the plugin with below code in constructor of the App Component .

     // Initialize the plugin
     window.plugins.openwith.init((initSuccess) => {
          console.log('init success!' + initSuccess);
     }, (initError) => {
          console.log('init failed: ' + initError);
     });

You can add event handlers then after..

     window.plugins.openwith.addHandler((intent) => {
           console.log('intent received');

           let fileType = '';
           for (const item of intent.items) {
              // const item = intent.items[i];
              console.log('  type: ', item.type);   // mime type
              console.log('  uri:  ', item.uri);
           }
     });

Hope this helps someone.. :) Happy Coding..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM