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:
--------------------------------------------------------------------
Using java Syntax Highlighting
- package ab.cd;
- import java.io.BufferedInputStream;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.URL;
- import android.app.Activity;
- import android.content.Intent;
- import android.net.Uri;
- import android.os.Bundle;
- import android.os.Environment;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- /*
- * program to send email with an jpeg attached...
- * similarly you can attach anything you want just make sure u r using the correct Type
- */
- public class SendMail extends Activity {
- Intent sendIntent;
- private File file;
- private Thread t;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- findViewById(R.id.Button01).setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- t.start();
- t.stop();
- }
- });
- t= new Thread(new Runnable() {
- @Override
- public void run() {
- file=getFile();
- Log.i("absol path", "file://"+file.getAbsolutePath());
- sendIntent = new Intent(Intent.ACTION_SEND);
- sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
- Log.i("parsing of URI", "start");
- sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+file.getAbsolutePath()));
- Log.i("parsing of URI", "stop");
- sendIntent.setType("image/jpeg");
- startActivity(Intent.createChooser(sendIntent, "MySendMail"));
- }
- });
- }
- private File getFile() {
- File f = null;
- String picstring= "http://wefunction.com/wp-content/uploads/2008/07/function_twitter_free.jpg";
- try {
- // Create file
- f=new File(Environment.getExternalStorageDirectory(), "outfile2.jpeg");
- InputStream inputStream= new BufferedInputStream(new URL(picstring)
- .openStream(),
- 1024*4);
- OutputStream out=new FileOutputStream(f);
- byte buf[]=new byte[1024];
- int len;
- while((len=inputStream.read(buf))>0)
- out.write(buf,0,len);
- out.close();
- inputStream.close();
- System.out.println("\nFile is created");
- }
- catch(Exception e){
- e.printStackTrace();
- }
- return f;
- }
- }
Parsed in 0.041 seconds, using GeSHi 1.0.8.4
-------------------------------------------------------------------------------------------
I hope this will help many woes!
happy coding



