Ionic Android Internal Shared Memory

1.7k Views Asked by At

I'm using Ionic v1 with $cordovaFile and cordova email plugin to create a file and then attach it to a new email. I have a working solution for iOS on any device, but I've come across a weird issue with Android.

Android File System Layout

When attempting to attach a file created in any internal storage location on Android (dataDirectory, etc), I receive "permission denied for attachment". However, when I save the file to external storage, the attachment is successfully added.

I have published my application using external storage for Android, but unfortunately some of today's most popular devices don't have external storage.

Does anyone know a solution? I've considered workarounds, such as uploading the file to a file-hosting service, and including a download link in the email. I would use $cordovaFileTransfer, but I don't know if there would be permissions errors in internal storage. I'd like to use a true email attachment if possible. Thanks!

2

There are 2 best solutions below

0
On

I think your issue is related to permission. I have implemented this for file attachment to give a permission for file attachment try it hope this help you. Try this plugin cordova.plugins.permissions

 function checkPermission() {
            var permissions = cordova.plugins.permissions;
            permissions.hasPermission(permissions.READ_EXTERNAL_STORAGE, checkPermissionCallback, null);
        }

function checkPermissionCallback(status) {
            $localStorage.StoragePermission = status.hasPermission;
            if (!status.hasPermission) {
                var permissions = cordova.plugins.permissions;
                permissions.requestPermission(permissions.READ_EXTERNAL_STORAGE, null, null);                
            }
        }
1
On

As the Issue is mainly related to the Permission Issue , before trying my second solution try to include this key in your config.xml

<preference name="AndroidPersistentFileLocation" value="Compatibility" />

From the Source it states that :

Previous versions of the plugin would choose the location of the temporary and persistent files on startup, based on whether the device claimed that the SD Card (or equivalent storage partition) was mounted. If the SD Card was mounted, or if a large internal storage partition was available (such as on Nexus devices,) then the persistent files would be stored in the root of that space. This meant that all Cordova apps could see all of the files available on the card.

If the SD card was not available, then previous versions would store data under /data/data/, which isolates apps from each other, but may still cause data to be shared between users.

It is now possible to choose whether to store files in the internal file storage location, or using the previous logic, with a preference in your application's config.xml file. To do this, add one of these two lines to config.xml:

<preference name="AndroidPersistentFileLocation" value="Internal" />

<preference name="AndroidPersistentFileLocation" value="Compatibility" />

Solution 2:

you can send file content in the base64 , try to integrate angular-base64 in your ionic-app and try to send the E-mail in the base64 format, here is the sample code :

$scope.tosendEmail= function(subject, message, file_content){
    var file = null;
    if(file_content != undefined){
        file = "base64:contact.vcf//"+$base64.encode(file_content);
    }

    var email_obj = {
      subject: subject,
      body: message,
      isHtml:false,
      attachments:file
    };

    cordova.plugins.email.open(email_obj);
}

So in this way you can send the attachments