Voting: 
Vote up!
0 Votes
8 Replies
Unanswered

convert image to raw bytes for email attachment

Question asked by ayou ixe on Feb 28, 2017 03:50 EST

Hi, I am trying to attach an image to an email using kony.phone.openEmail The image is generated from kony.image.createImageFromSnapshot . I found this reference to properly attach an image to the email. http://docs.kony.com/konylibrary/visualizer/viz_api_dev_guide/content/sharefilesandroid.htm#Specify   I followed it and this is my code: function sharePay(){        if(frmMRCodeGen.browserQR.isVisible){       var imgSnapshot = kony.image.createImageFromSnapShot(frmMRCodeGen.browserQR);     } else {       var imgSnapshot = kony.image.createImageFromSnapShot(frmMRCodeGen.browser2D);     }              frmMRCodeGen.imgSnap.image = imgSnapshot;          var imgRawBytes = imgSnapshot.getImageAsRawBytes();     var path = kony.io.FileSystem.getDataDirectoryPath();     var sharedDir = path+constants.FILE_PATH_SEPARATOR+"images";     var sharefolder = new kony.io.File(sharedDir).createDirectory();     var fileLoc = sharedDir+constants.FILE_PATH_SEPARATOR+"myimage.png";     var myfile=new kony.io.File(fileLoc).createFile();     var write=new kony.io.File(fileLoc).write(imgRawBytes);     var imgAttach=new kony.io.File(fileLoc).read();          var torecipients = [];       var ccrecipients = [];     var bccrecipients = [];     var subject = "BDO Pay Anyone - Send";     var messagebody = "Hi, you have been transferred money. Get it now.";           var attachments = [];        attachments.push({         mimetype   : "image/png",         attachment : imgAttach,     });       try{           kony.phone.openEmail(torecipients, ccrecipients, bccrecipients, subject, messagebody, false, [attachments]);     } catch(err) {       alert(err);     } }   But it seems that the image is not converted into rawBytes. And will not attach the image to the email. A toast pops up saying "Can't attach empty file"   Can you please refer me to another way to convert the image into raw bytes?  

Vote up!
0 votes
Vote down!

Hi,

I would also like to clarify the step below:

http://docs.kony.com/konylibrary/visualizer/viz_api_dev_guide/content/sharefilesandroid.htm#Specify

Specifying the FileProvider in the Manifest Properties

I followed the steps to sharing files on android. I created an xml named filespath.xml with the following content:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<paths>
   <files-path path="images/" name="myimages"/>
</paths>

I added the following to the Application Tag Attributes:

<provider   android:name="android.support.v4.content.FileProvider"   android:authorities="com.bdo.pay.anyone.mobile.fileprovider"   android:exported="false"   android:grantUriPermissions="true">   <meta-data      android:name="android.support.FILE_PROVIDER_PATHS"      android:resource="@xml/filepaths" /> </provider>

I am getting an error when building the app.

[Fatal Error] :5:82: Element type "application" must be followed by either attribute specifications, ">" or "/>".

Please advise.

 

Replied by: Feb 28, 2017 22:46 EST
Vote up!
0 votes
Vote down!

I'm having trouble myself with the read() function: var imgAttach=new kony.io.File(fileLoc).read(); I'm getting the idea that function does not work or some conversion hast to be done.

Does it work when you just use imgRawBytes as the attachment?

Replied by: Mar 02, 2017 08:32 EST
Vote up!
0 votes
Vote down!

 

 

Replied by: Mar 02, 2017 08:44 EST
Vote up!
0 votes
Vote down!

Hi Roelof Koelewijn,

 

No, it doesn't work. imgRawBytes returns empty or null but I don't get why.

 frmMRCodeGen.imgSnap.image = imgSnapshot; 

This clearly works as the image is shown in the app but when I try to convert it using getImageAsRawBytes, it returns empty of null. I've tried using base64, rawBytes, but it still does not convert my image, either from the image widget or the image creates from snapshot, into rawbytes.

Replied by: Mar 02, 2017 21:41 EST
Vote up!
0 votes
Vote down!

Hi Roelof Koelewijn,

 

I tried your fix

var imgAttach=JSON.stringify(new kony.io.File(fileLoc).read());

It doesn't work for me. Still shows a toast "Can't attach empty file". I believe the root cause is the image conversion to raw bytes since it does not convert into raw bytes

Replied by: Mar 02, 2017 22:27 EST
Vote up!
0 votes
Vote down!

Maybe you could try:  var imgAttach = frmMRCodeGen.imgSnap.rawBytes

Replied by: Mar 03, 2017 01:13 EST
Vote up!
0 votes
Vote down!

I've already tried that. I also tried base64. I also tried kony.convertToBase64 and kony.convertToRawBytes.

Somehow, I've been finding some threads that say that only images taken using the Camera API can be converted into raw bytes. Namely this one, it stands out a lot.

http://community.kony.com/developer/forum/convert-image-src-base64

Replied by: Mar 03, 2017 04:08 EST
Vote up!
0 votes
Vote down!

Hi Kony,

Any update on this?

Replied by: Mar 15, 2017 01:15 EDT