无法使用 Ionic2/3 社交分享分享图片
Unable to share image using Ionic2/3 Social Share
在这里,我首先使用 ionic social share 分享一张图片,我所做的是使用 Canvas 抓取图片,并将相同的图片代码导出到分享插件。当我这样做时,我得到了下面的内容,我的意思是我正在通过电子邮件发送
'data:image/png;base64,R0lGODlhDAAMALMBAP8AAP///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAUKAAEALAAAAAAMAAwAQAQZMMhJK7iY4p3nlZ8XgmNlnibXdVqolmhcRQA7'
下面是我的代码:
Home.html
<canvas #myCanvas>
<ion-card>
<img [src]="image">
<ion-card-content>
<ion-card-title>
Nine Inch Nails Live
</ion-card-title>
<p>
The most popular industrial group ever, and largely
responsible for bringing the music to a mass audience.
</p>
</ion-card-content>
</ion-card>
</canvas>
<button ion-button (click)="getDat()"></button>
.ts代码
@ViewChild('myCanvas') myCanvas:ElementRef ;
image:any = 'https://images.pexels.com/photos/6966/abstract-music-rock-bw.jpg?auto=compress&cs=tinysrgb&h=350';
constructor(public navCtrl: NavController,public share:SocialSharing) {
}
getDat(){
var canvas = this.myCanvas.nativeElement;
var imageBase64Data = canvas.toDataURL("image/png");
console.log(imageBase64Data)
this.share.canShareViaEmail().then(() => {
this.share.shareViaEmail(imageBase64Data,'IDCARD', ['trend@gmail.co']).then(() => {
;
}).catch(() => {
// Error!
});
}).catch(() => {
// Sharing via email is not possible
});
}
所以可以在正文中发送图像
您可以使用电子邮件编辑器添加附件。请参阅下面的代码。
import { EmailComposer } from '@ionic-native/email-composer';
constructor(private emailComposer: EmailComposer) { }
...
this.emailComposer.isAvailable().then((available: boolean) =>{
if(available) {
//Now we know we can send
}
});
let email = {
to: 'max@mustermann.de',
cc: 'erika@mustermann.de',
bcc: ['john@doe.com', 'jane@doe.com'],
attachments: [
'file://img/logo.png',
'res://icon.png',
'base64:icon.png//iVBORw0KGgoAAAANSUhEUg...',
'file://README.pdf'
],
subject: 'Cordova Icons',
body: 'How are you? Nice greetings from Leipzig',
isHtml: true
};
// Send a text message using default options
this.emailComposer.open(email);
}
更多信息请查看https://ionicframework.com/docs/native/email-composer/
更新
您可以将其用于社交分享
socialsharing.shareViaEmail(
'Message', // can contain HTML tags, but support on Android is rather limited:
'Subject',
['to@person1.com', 'to@person2.com'], // TO: must be null or an array
['cc@person1.com'], // CC: must be null or an array
null, // BCC: must be null or an array
['https://www.google.nl/images/srpr/logo4w.png','www/localimage.png'], // FILES: can be null, a string, or an array
onSuccess, // called when sharing worked, but also when the user cancelled sharing via email. On iOS, the callbacks' boolean result parameter is true when sharing worked, false if cancelled. On Android, this parameter is always true so it can't be used). See section "Notes about the successCallback" below.
onError // called when sh*t hits the fan
);
在这里,我首先使用 ionic social share 分享一张图片,我所做的是使用 Canvas 抓取图片,并将相同的图片代码导出到分享插件。当我这样做时,我得到了下面的内容,我的意思是我正在通过电子邮件发送
'data:image/png;base64,R0lGODlhDAAMALMBAP8AAP///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAUKAAEALAAAAAAMAAwAQAQZMMhJK7iY4p3nlZ8XgmNlnibXdVqolmhcRQA7'
下面是我的代码: Home.html
<canvas #myCanvas>
<ion-card>
<img [src]="image">
<ion-card-content>
<ion-card-title>
Nine Inch Nails Live
</ion-card-title>
<p>
The most popular industrial group ever, and largely
responsible for bringing the music to a mass audience.
</p>
</ion-card-content>
</ion-card>
</canvas>
<button ion-button (click)="getDat()"></button>
.ts代码
@ViewChild('myCanvas') myCanvas:ElementRef ;
image:any = 'https://images.pexels.com/photos/6966/abstract-music-rock-bw.jpg?auto=compress&cs=tinysrgb&h=350';
constructor(public navCtrl: NavController,public share:SocialSharing) {
}
getDat(){
var canvas = this.myCanvas.nativeElement;
var imageBase64Data = canvas.toDataURL("image/png");
console.log(imageBase64Data)
this.share.canShareViaEmail().then(() => {
this.share.shareViaEmail(imageBase64Data,'IDCARD', ['trend@gmail.co']).then(() => {
;
}).catch(() => {
// Error!
});
}).catch(() => {
// Sharing via email is not possible
});
}
所以可以在正文中发送图像
您可以使用电子邮件编辑器添加附件。请参阅下面的代码。
import { EmailComposer } from '@ionic-native/email-composer';
constructor(private emailComposer: EmailComposer) { }
...
this.emailComposer.isAvailable().then((available: boolean) =>{
if(available) {
//Now we know we can send
}
});
let email = {
to: 'max@mustermann.de',
cc: 'erika@mustermann.de',
bcc: ['john@doe.com', 'jane@doe.com'],
attachments: [
'file://img/logo.png',
'res://icon.png',
'base64:icon.png//iVBORw0KGgoAAAANSUhEUg...',
'file://README.pdf'
],
subject: 'Cordova Icons',
body: 'How are you? Nice greetings from Leipzig',
isHtml: true
};
// Send a text message using default options
this.emailComposer.open(email);
}
更多信息请查看https://ionicframework.com/docs/native/email-composer/
更新
您可以将其用于社交分享
socialsharing.shareViaEmail(
'Message', // can contain HTML tags, but support on Android is rather limited:
'Subject',
['to@person1.com', 'to@person2.com'], // TO: must be null or an array
['cc@person1.com'], // CC: must be null or an array
null, // BCC: must be null or an array
['https://www.google.nl/images/srpr/logo4w.png','www/localimage.png'], // FILES: can be null, a string, or an array
onSuccess, // called when sharing worked, but also when the user cancelled sharing via email. On iOS, the callbacks' boolean result parameter is true when sharing worked, false if cancelled. On Android, this parameter is always true so it can't be used). See section "Notes about the successCallback" below.
onError // called when sh*t hits the fan
);