簡體   English   中英

如何將圖像從外部應用程序共享到 Ionic 4 應用程序

[英]How to share an image from external app to an Ionic 4 app

我正在嘗試使用“共享”function 從另一個應用程序到我的 Ionic 4 應用程序共享圖像。 我的應用程序必須處理圖像並使用它。 我已經嘗試過這個解決方案,但它適用於 Ionic 3。插件似乎不再被維護。 在 Ionic 4 中必須有這樣一個常見任務的解決方案。

我試過darryncampbell-cordova-plugin-intent沒有成功。 我的代碼是

    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...

這段代碼什么都不做。 什么都沒有。 我能夠編輯我的 config.xml 來配置清單,以便我的應用程序顯示在帶有菜單的共享中。 這很好用。

<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>

我知道這是 OP 的重播,但我認為它會對某人有所幫助..

我使用了cordova-plugin-openwith插件來實現這一點。 這將幫助您使用“共享方式”function 從另一個應用程序共享圖像到我們的 Ionic 4 應用程序。

您可以在App Component的構造函數中使用以下代碼初始化插件。

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

您可以在之后添加事件處理程序..

     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);
           }
     });

希望這對某人有所幫助.. :) 快樂編碼..

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM