How to attach file to email in Ionic Framework Android? - android

I'm using Ionic Framework to build an iOS / Android app which writes sqlite data to a CSV file, then attaches that file to an Email and sends it. The following code works correctly on iOS (actual device iPhone 5).
I don't have an Android device, but in the Android emulator (nexus 5), the file sent never has a file attachment (despite the emulator showing that it does).
Is there a different way I should be writing this code?
I looked at the documentation here, but it does not clarify
https://github.com/katzer/cordova-plugin-email-composer#adding-attachments
$cordovaFile.writeFile(cordova.file.dataDirectory,
"PatientEncounters.csv",
data.join("\n"),
true)
.then(function (success) {
$cordovaEmailComposer.isAvailable().then(function() {
var emailOpts = {
to: [email],
attachments: ['' +
cordova.file.dataDirectory.replace('file://','') + "PatientEncounters.csv"],
subject: 'Patient Encounters',
body: 'A CSV containing Patient Encounters is attached',
isHtml: false
};
$cordovaEmailComposer.open(emailOpts).then(null, function () {
// user cancelled email
});
return;
}, function (error) {
return;
});
}, function () {
// not available
});

My problem was using cordova.file.dataDirectory instead of cordova.file.externalDataDirectory. The mail app in android would not allow attaching files from internal storage.

Related

React native open default mail app with attachments

I need to be able to open the default email app on the click of a button from a react native application. I tried using react-native-email-link but it does not have the option of adding attachments. Is there something else I can use that will allow attachments?
You can likely achieve this with expo-mail-composer:
import * as MailComposer from 'expo-mail-composer';
[...]
const canMail = await MailComposer.isAvailableAsync();
if(!canMail) {
console.warn("Can't open the mail app! You may be using the iPhone simulator or don't have a mail app installed")
return;
}
MailComposer.composeAsync({
subject: "Hello, Mail Composer!",
body: "Some body text goes here",
recipients: "somebody#example.com",
attachments: [
"https://venturebeat.com/wp-content/uploads/2017/09/stack_overflow_logo.jpg?w=1200&strip=all"
// you can also attach local files
]
});
Working Snack demo here: https://snack.expo.dev/#joem-rp/expo-mail-composer-demo

How to make ionic application read in json data from .json file that is local to application

I have a Cordova app that uses the ionic framework. I have many json data files that I put in the www/json folder in my app's file tree. I am using angularJS http calls to access them.
When I test my app in chrome (using "ionic serve" in the terminal) it works fine but when I test it on an android device(nexus 5 with Android 6.0 Marshmallow) it no longer works.
Sample code
function getBookNames() {
return $http.get("..\\bookfolder\\Books.json")
.then(function (data) {//if success than do
return data.data;
}, function (reason) {// if fail than do
// maybe tell the user what happened...
});
}
I have tried adding
var path = "";
if (ionic.Platform.isAndroid()) {
path = "\\android_asset\\www\\";
}
function getBookNames() {
return $http.get(path + "..\\bookfolder\\Books.json")
.then(function (data) {//if success than do
return data.data;
}, function (reason) {// if fail than do
// maybe tell the user what happened...
});
}
when I am debugging the app on my phone I get the following error.
GET file:///android_asset/bookfolder/Books.json net::ERR_FILE_NOT_FOUND
If anyone knows what I am doing wrong or of a better way to access local .json files please let me know. Thanks!
Use slash / character, not backslash \. Also, put bookfolder inside /android_asset/www
$http
.get('/android_asset/www/bookfolder/books.json')
.then(function(response){ return response.data });

GET a collection fails on phone but works fine on computer

My code fetches the apigee collection fine when running on the desktop with Chrome browser.
When I turn it into an app via phonegap/android-studio and run it on my android phone it shows my alert: "error fetching remote candidate data" that means fetch returned an error.
Why would this fetch the collection on the desktop but not the phone?
(guest has GET permissions for the collection)
function loadRemoteData() {
endorseds = new Apigee.Collection({
"client": client,
"type": "endorseds",
"qs": {
"limit": 4000,
"ql": "order by order,district,name"
}
});
$.mobile.loading("show");
endorseds.fetch(function(err, data) {
if (err) { alert("error fetching remote candidate data ");
if(lastLocalDate > 0){
loadLocalData();
}
$.mobile.loading("hide");
} else {
alert("remote fetch ok");
localStorage.setItem('endorseds', endorseds.serialize());
if(lastRemoteDate == 0) lastRemoteDate = getToday();
localStorage.setItem('lastdate',lastRemoteDate);
$.mobile.loading("hide");
showMain();
}
});
}
The answer depends upon the version of PhoneGap that you are using. Have a look at the config.xml file. At the bottom (by default) of the file, you should see a line where "something here" was 127.0.0.1/* in older versions of PhoneGap. I'm not sure where it got change (3.2, I believe), but it is now which allows the app to communicate with any web site/application. Change that line to the wildcard and recompile – this should solve the issue.

Meteor accounts-facebook broken on Android?

I have created a meteor app and added the accounts-facebook package. I can login to the site using {{loginButtons}} fine from chrome (on Linux locally and from another machine) - however, when I try and login via Android (on the same network), the facebook popup reports: HTTP 500 error.
My serverside js looks like this:
Meteor.startup(function () {
// code to run on server at startup
try{
Accounts.loginServiceConfiguration.remove({
service : "facebook"
});
Accounts.loginServiceConfiguration.insert({
service : "facebook",
appId : xxxx,
secret : xxxxxxxxxxxxxxxxxx
});
Accounts.onCreateUser(function(options, user) {
console.log('creating user');
//Do create stuff here
// We still want the default hook's 'profile' behavior.
if (options.profile)
user.profile = options.profile;
return user;
});
}catch(err){
console.log(err.message);
}
});
Any suggestions?
Many thanks.

How to create a generic "create photo" code for both iPhone and Android using Appcelerator?

I am trying to create an application that is capable of creating photos on both Android and iPhone using Appcelerator. The functionality should launch default photo API, create a photo, allow user to either agree with photo, or cancel it, and on successful result save it on the memory card. It is not necessary that photos should be automatically added to the Gallery.
Currently I am using this code which works perfectly for Android :
Rf.media.photo = {
key: 'photo',
title: 'Photo',
extension: 'jpg',
type: 'image/jpeg',
create: function(created) {
Ti.Media.showCamera({
// TODO: disallow video for ios
animated: false,
saveToPhotoGallery: false,
showControls: true,
success: function(media_item) {
var name = Rf.util.timestamp() + '.' + Rf.media.photo.extension;
Rf.write_to_new_file(name, media_item.media, function(file) {
created(file);
});
},
});
},
};
I am looking for ways to tweek this code, so it would work also for iPhone. At the moment it is unresponsive when tested on iPhone 4.
Anyone knows whats wrong with it?
take a look at the kitchenSink examples for photo and photo gallery, they provide a pretty complete example

Categories

Resources