Mark Harrop Mark Harrop - 2 years ago 84
Android Question

how would i send a picture as part of an email progmaticly

hi guys making a simple email app for a friend.. it will send him info for part of his business. but it also sometimes needs a photo attaching. ive solved the opening of the camera and taking and saving the picture to the devices memory using this piece of code

public class CameraDemoActivity extends Activity {
public static int count = 0;

/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState)

// Here, we are making a folder named picFolder to store
// pics taken by the camera using this application.
final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolder/";
File newdir = new File(dir);

Button capture = (Button) findViewById(;
capture.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {

// Here, the counter will be incremented each time, and the
// picture taken by camera will be stored as 1.jpg,2.jpg
// and likewise.
String file = dir+count+".jpg";
File newfile = new File(file);
try {
catch (IOException e)

Uri outputFileUri = Uri.fromFile(newfile);

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);

startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {
Log.d("CameraDemo", "Pic saved");


how would i now attach this to an email with in the app? any help always appreciated thanks

Answer Source

As i guess you are going to use intent to send the mail, just add this while building your intent :

intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download