Email Attachments for Android

Hi all,

I’m developing an app that uses backup through email file. when the user need to make a backup can send a file from the app that he/she can save. After time, if need to restore the backup, only need to open the file in the app.

For that, I have follow the tutorial https://coronalabs.com/blog/2013/02/05/ios-tutorial-using-email-attachments/ that explain how to get email attachments at iOS.

But, for Android, I’m turning crazy.

I have configure the intent-filters to open the app with certain file extensions, but, no chance for import the file.

I have used network.download to do it, but appear to not work.

Any one have try any solution for this?

Android send the url to the app like “content://downloads/all_downloads/692”

if a file send: “file://location…”

I’d love if any one can help me to get these files with this url.

Thanks in advance,


– main.lua


local lfs = require( “lfs” )

local launchArgs = …

local launchFile = “none”;

local fileName;

display.newText(“File passed to system.DocumentsDirectory:”, display.contentWidth * .5, 50, native.systemFont, 12);

fileName = display.newText(launchFile, 10, 200, display.contentWidth, display.contentHeight * .5, native.systemFont, 12);

fileName:setFillColor( 1, 0, 0)

fileName.anchorX = 0

fileName.anchorY = 0.5

local function muestradirectorios()

local directorios = “”

– Get raw path to the app documents directory

local doc_path = system.pathForFile( “”, system.DocumentsDirectory )

for file in lfs.dir( doc_path ) do

   – “file” is the current file or directory name

   print( "Found file: " … file )

   directorios = directorios … " - documents - " … file … " – "

end

– Get raw path to the app documents directory

local doc_path = system.pathForFile( “”, system.TemporaryDirectory )

 

for file in lfs.dir( doc_path ) do

   – “file” is the current file or directory name

   print( "Found file: " … file )

   directorios = directorios … " - temporary - " … file … " – "

end

return directorios

end

local function handleFile(argumentos)

if (argumentos and argumentos.url) then

   miFile= argumentos.url

    --descarga file

if (string.sub(miFile, 1, 7) == “file://”) then – handle custom extension

–it is a file… need to copy it to temporary.directory…

launchFile = "FILE --> " … miFile

fileName.text = launchFile

else – handle URL Scheme

– do something es URL

launchFile = "URL --> " … miFile

fileName.text = launchFile

local function networkListener( event )

   if ( event.isError ) then

       print( "Network error - download failed: ", event.response )

       launchFile = “ERROR”

       fileName.text = launchFile

   elseif ( event.phase == “began” ) then

       print( “Progress Phase: began” )

       launchFile = “BEGAN”

       fileName.text = launchFile

   elseif ( event.phase == “ended” ) then

 

    --listo directorios

       launchFile = "URL: " … argumentos.url … " -END- " … muestradirectorios()

       fileName.text = launchFile

   else

    launchFile = “OTRO RARO QUE NO ESTA CONTEMPLADO”

    fileName.text = launchFile

   end

end

network.download( miFile, “GET”, networkListener, {timeout=10, progress=true}, “archivodescargado.cnk”, system.TemporaryDirectory )

end

end

end

handleFile(launchArgs)

Hello,
did you solve the problem?
I need to do same and got stuck with the same issues…
Thx in advance

Hi, sorry for delay. I just see your question.

No dear. Terrible, I don’t find the way to do it.

Finally decide to use other way  (external Storage plugging to save to an external SDCard.

Best regards,

Thanks for replying.  I’ve  read your thread several times hoping to glean something else to try.

I too am trying to open an email attachment with a custom file extension with an android app and iterate over it.  I can do it with iOS but not Android.

I have struggled trying different combinations of Intent actions, categories and data schemes.  Just can’t get Android 4.4.4 to recognize the file extension and launch my app.

So frustrated about this.

Nail

Use the below code to  Email Attachments for Android

String filename=“contacts_sid.vcf”;
File filelocation = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), filename);
Uri path = Uri.fromFile(filelocation);
Intent emailIntent = new Intent(Intent.ACTION_SEND);
// set the type to ‘email’
emailIntent .setType(“vnd.android.cursor.dir/email”);
String to[] = {“asd@gmail.com”};
emailIntent .putExtra(Intent.EXTRA_EMAIL, to);
// the attachment
emailIntent .putExtra(Intent.EXTRA_STREAM, path);
// the mail subject
emailIntent .putExtra(Intent.EXTRA_SUBJECT, “Subject”);
startActivity(Intent.createChooser(emailIntent , “Send email…”));

Thanks so much all.

I have been playing with this.

The Android solution as sharrahul425 mention, is to open an input stream to get the file from the URI.

Need to do something with the url we get.

I have an url of this kind:

content://gmail-ls/…messages/593/attachments/0.1.1/BEST/false

android documentation explain need to get with something similar to this:

activity.getContentResolver().openInputStream(uri)

Any chance to do it in lua, corona?

Thanks again


– main.lua


local lfs = require( “lfs” )

local launchArgs = …

local launchFile = “none”;

local fileName;

display.newText(“File passed to system.DocumentsDirectory:”, display.contentWidth * .5, 50, native.systemFont, 12);

fileName = display.newText(launchFile, 10, 200, display.contentWidth, display.contentHeight * .5, native.systemFont, 12);

fileName:setFillColor( 1, 0, 0)

fileName.anchorX = 0

fileName.anchorY = 0.5

local function muestradirectorios()

local directorios = “”

– Get raw path to the app documents directory

local doc_path = system.pathForFile( “”, system.DocumentsDirectory )

for file in lfs.dir( doc_path ) do

   – “file” is the current file or directory name

   print( "Found file: " … file )

   directorios = directorios … " - documents - " … file … " – "

end

– Get raw path to the app documents directory

local doc_path = system.pathForFile( “”, system.TemporaryDirectory )

 

for file in lfs.dir( doc_path ) do

   – “file” is the current file or directory name

   print( "Found file: " … file )

   directorios = directorios … " - temporary - " … file … " – "

end

return directorios

end

local function handleFile(argumentos)

if (argumentos and argumentos.url) then

   miFile= argumentos.url

    --descarga file

if (string.sub(miFile, 1, 7) == “file://”) then – handle custom extension

–it is a file… need to copy it to temporary.directory…

launchFile = "FILE --> " … miFile

fileName.text = launchFile

else – handle URL Scheme

– do something es URL

launchFile = "URL --> " … miFile

fileName.text = launchFile

local function networkListener( event )

   if ( event.isError ) then

       print( "Network error - download failed: ", event.response )

       launchFile = “ERROR”

       fileName.text = launchFile

   elseif ( event.phase == “began” ) then

       print( “Progress Phase: began” )

       launchFile = “BEGAN”

       fileName.text = launchFile

   elseif ( event.phase == “ended” ) then

 

    --listo directorios

       launchFile = "URL: " … argumentos.url … " -END- " … muestradirectorios()

       fileName.text = launchFile

   else

    launchFile = “OTRO RARO QUE NO ESTA CONTEMPLADO”

    fileName.text = launchFile

   end

end

network.download( miFile, “GET”, networkListener, {timeout=10, progress=true}, “archivodescargado.cnk”, system.TemporaryDirectory )

end

end

end

handleFile(launchArgs)

Hello,
did you solve the problem?
I need to do same and got stuck with the same issues…
Thx in advance

I’m so glad to introduce you all my new plugin that can manage Uri in Android.

With this plugin its so easy to get the files opened with our app as discussed at this forum. I’m preparing the code and will share it soon.

The plugin (actual version) allow to pick a file using the android dialog.

Also allow to get an external file knowing it’s uri.

And can share a file in a simple way.

It is here:

https://marketplace.coronalabs.com/plugin/android-file-and-uri-manager

I’m preparing some samples to show its functionality. For example, with this plugin it is so easy to get a file (custom extension) that it is open with our app.

Hope it can be useful for the community!

Best regards, and thanks for allow me to be here.

Hi, sorry for delay. I just see your question.

No dear. Terrible, I don’t find the way to do it.

Finally decide to use other way  (external Storage plugging to save to an external SDCard.

Best regards,

Thanks for replying.  I’ve  read your thread several times hoping to glean something else to try.

I too am trying to open an email attachment with a custom file extension with an android app and iterate over it.  I can do it with iOS but not Android.

I have struggled trying different combinations of Intent actions, categories and data schemes.  Just can’t get Android 4.4.4 to recognize the file extension and launch my app.

So frustrated about this.

Nail

Use the below code to  Email Attachments for Android

String filename=“contacts_sid.vcf”;
File filelocation = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), filename);
Uri path = Uri.fromFile(filelocation);
Intent emailIntent = new Intent(Intent.ACTION_SEND);
// set the type to ‘email’
emailIntent .setType(“vnd.android.cursor.dir/email”);
String to[] = {“asd@gmail.com”};
emailIntent .putExtra(Intent.EXTRA_EMAIL, to);
// the attachment
emailIntent .putExtra(Intent.EXTRA_STREAM, path);
// the mail subject
emailIntent .putExtra(Intent.EXTRA_SUBJECT, “Subject”);
startActivity(Intent.createChooser(emailIntent , “Send email…”));

Thanks so much all.

I have been playing with this.

The Android solution as sharrahul425 mention, is to open an input stream to get the file from the URI.

Need to do something with the url we get.

I have an url of this kind:

content://gmail-ls/…messages/593/attachments/0.1.1/BEST/false

android documentation explain need to get with something similar to this:

activity.getContentResolver().openInputStream(uri)

Any chance to do it in lua, corona?

Thanks again

Cesar,

Thank You so much for sharing your solution and taking the time to make your awesome plugin.  I was dead in the water with my Android version until I revisited this post recently and saw your plugin announcement.

I’ve promoted your plugin in this thread.

https://forums.coronalabs.com/topic/70795-any-idea-how-to-exchange-data-between-users/

Nail

I’m so glad to introduce you all my new plugin that can manage Uri in Android.

With this plugin its so easy to get the files opened with our app as discussed at this forum. I’m preparing the code and will share it soon.

The plugin (actual version) allow to pick a file using the android dialog.

Also allow to get an external file knowing it’s uri.

And can share a file in a simple way.

It is here:

https://marketplace.coronalabs.com/plugin/android-file-and-uri-manager

I’m preparing some samples to show its functionality. For example, with this plugin it is so easy to get a file (custom extension) that it is open with our app.

Hope it can be useful for the community!

Best regards, and thanks for allow me to be here.

Cesar,

Thank You so much for sharing your solution and taking the time to make your awesome plugin.  I was dead in the water with my Android version until I revisited this post recently and saw your plugin announcement.

I’ve promoted your plugin in this thread.

https://forums.coronalabs.com/topic/70795-any-idea-how-to-exchange-data-between-users/

Nail