send email with attached image from remote server

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

send email with attached image from remote server

Postby arin » Tue Dec 22, 2009 6:39 pm

Hi all,


I was looking for a specific solutions or tips about how to send emails with attached jpeg downloaded from the remote server on the fly... I am new to android and I din't have any clue at first. Nor I could able to find a clear code snippet which allows me to achieve this.

Anyway after 3 hours of research I finally made it. The following code is working fine in my G1 device, although only email programs such as android's native email app, gmail app etc. are supported, If I tried to use this with the facebook app, it failed...

I will be more than happy to welcome any suggestions (yes I love criticism)

here's what I did:

--------------------------------------------------------------------
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package ab.cd;
  2.  
  3.  
  4.  
  5. import java.io.BufferedInputStream;
  6.  
  7. import java.io.File;
  8.  
  9. import java.io.FileOutputStream;
  10.  
  11. import java.io.InputStream;
  12.  
  13. import java.io.OutputStream;
  14.  
  15. import java.net.URL;
  16.  
  17.  
  18.  
  19. import android.app.Activity;
  20.  
  21. import android.content.Intent;
  22.  
  23. import android.net.Uri;
  24.  
  25. import android.os.Bundle;
  26.  
  27. import android.os.Environment;
  28.  
  29. import android.util.Log;
  30.  
  31. import android.view.View;
  32.  
  33. import android.view.View.OnClickListener;
  34.  
  35.  
  36.  
  37. /*
  38.  
  39.  * program to send email with an jpeg attached...
  40.  
  41.  * similarly you can attach anything you want just make sure u r using the correct Type
  42.  
  43.  */
  44.  
  45. public class SendMail extends Activity {
  46.  
  47.         Intent sendIntent;
  48.  
  49.         private File file;
  50.  
  51.         private Thread t;
  52.  
  53.        
  54.  
  55.     /** Called when the activity is first created. */
  56.  
  57.     @Override
  58.  
  59.     public void onCreate(Bundle savedInstanceState) {
  60.  
  61.         super.onCreate(savedInstanceState);
  62.  
  63.         setContentView(R.layout.main);
  64.  
  65.        
  66.  
  67.         findViewById(R.id.Button01).setOnClickListener(new OnClickListener() {
  68.  
  69.                        
  70.  
  71.                         @Override
  72.  
  73.                         public void onClick(View v) {                          
  74.  
  75.                                 t.start();
  76.  
  77.                                 t.stop();
  78.  
  79.                         }
  80.  
  81.                 });
  82.  
  83.        
  84.  
  85.         t= new Thread(new Runnable() {
  86.  
  87.                        
  88.  
  89.                         @Override
  90.  
  91.                         public void run() {
  92.  
  93.                                 file=getFile();
  94.  
  95.                                  Log.i("absol path", "file://"+file.getAbsolutePath());
  96.  
  97.                                 sendIntent = new Intent(Intent.ACTION_SEND);
  98.  
  99.                                 sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
  100.  
  101.                                
  102.  
  103.                                 Log.i("parsing of URI", "start");
  104.  
  105.                                 sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+file.getAbsolutePath()));
  106.  
  107.                                 Log.i("parsing of URI", "stop");
  108.  
  109.                                
  110.  
  111.                                 sendIntent.setType("image/jpeg");
  112.  
  113.                                 startActivity(Intent.createChooser(sendIntent, "MySendMail"));
  114.  
  115.                                
  116.  
  117.                         }
  118.  
  119.                 });
  120.  
  121.     }
  122.  
  123.  
  124.  
  125.         private File getFile() {
  126.  
  127.                
  128.  
  129.                 File f = null;
  130.  
  131.                 String picstring= "http://wefunction.com/wp-content/uploads/2008/07/function_twitter_free.jpg";
  132.  
  133.                
  134.  
  135.                 try {
  136.  
  137.                         // Create file                 
  138.  
  139.                         f=new File(Environment.getExternalStorageDirectory(), "outfile2.jpeg");
  140.  
  141.                    
  142.  
  143.                         InputStream inputStream= new BufferedInputStream(new URL(picstring)
  144.  
  145.                     .openStream(),
  146.  
  147.                                         1024*4);
  148.  
  149.                     OutputStream out=new FileOutputStream(f);
  150.  
  151.                     byte buf[]=new byte[1024];
  152.  
  153.                     int len;
  154.  
  155.                     while((len=inputStream.read(buf))>0)
  156.  
  157.                     out.write(buf,0,len);
  158.  
  159.                     out.close();
  160.  
  161.                     inputStream.close();
  162.  
  163.                     System.out.println("\nFile is created");
  164.  
  165.                 }
  166.  
  167.                 catch(Exception e){
  168.  
  169.                         e.printStackTrace();
  170.  
  171.                 }
  172.  
  173.                 return f;
  174.  
  175.         }
  176.  
  177. }
  178.  
  179.  
Parsed in 0.042 seconds, using GeSHi 1.0.8.4

-------------------------------------------------------------------------------------------

I hope this will help many woes!

happy coding :wink:
arin
Junior Developer
Junior Developer
 
Posts: 13
Joined: Thu Jul 09, 2009 10:17 am

Top

Postby jiveturducken » Fri Apr 23, 2010 3:26 am

So why does facebook fail? I'm doing something similar (code below) yet every time facebook fails when trying to attach the image. Handcent also has problems adding the image yet the normal messaging app and gmail both work fine. I see no errors in logcat as to what went wong either.

Photo Viewer:
Code: Select all
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + saveFileName));
sendIntent.setType("image/jpeg");
startActivity(Intent.createChooser(sendIntent,"Send Image To:"));


Manfiest:
Code: Select all
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
jiveturducken
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Apr 23, 2010 3:16 am

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 5 guests