Android: How to send an image as email attachment from application?

后端 未结 3 842
慢半拍i
慢半拍i 2020-12-09 06:56

I\'m currently trying to create an app that will take a picture and then attach that picture to an email that will be going to a pre determined email address.

I hav

相关标签:
3条回答
  • 2020-12-09 07:31
    import android.os.Bundle;
    import android.app.Activity;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.support.v4.app.NavUtils;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    
    
    import android.content.ContentValues;
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.graphics.Bitmap.CompressFormat;
    import android.net.Uri;
    
    import android.provider.MediaStore.Images;
    import android.provider.MediaStore.Images.Media;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ImageView;
    
    
     public class MainActivity extends Activity {
      Button send;
      Bitmap thumbnail;
      File pic;
      EditText address, subject, emailtext;
      protected static final int CAMERA_PIC_REQUEST = 0;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        send=(Button) findViewById(R.id.emailsendbutton);
        address=(EditText) findViewById(R.id.emailaddress);
        subject=(EditText) findViewById(R.id.emailsubject);
        emailtext=(EditText) findViewById(R.id.emailtext);
    
    
    
    
    
    
    
    
    
    
        Button camera = (Button) findViewById(R.id.button1); 
        camera.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View arg0){
                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);  
    
            }
            });
    
            send.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View arg0){
    
                Intent i = new Intent(Intent.ACTION_SEND);
                i.putExtra(Intent.EXTRA_EMAIL, new String[]{"fake@fake.edu"});
                i.putExtra(Intent.EXTRA_SUBJECT,"On The Job");
                //Log.d("URI@!@#!#!@##!", Uri.fromFile(pic).toString() + "   " + pic.exists());
                i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(pic));
    
                i.setType("image/png");
                startActivity(Intent.createChooser(i,"Share you on the jobing"));
            }
            });
    
    
    } 
    
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
        if (requestCode == CAMERA_PIC_REQUEST) {  
         thumbnail = (Bitmap) data.getExtras().get("data");  
        ImageView image = (ImageView) findViewById(R.id.imageView1);  
        image.setImageBitmap(thumbnail);
    
    
            try {
                File root = Environment.getExternalStorageDirectory();
                if (root.canWrite()){
                     pic = new File(root, "pic.png");
                    FileOutputStream out = new FileOutputStream(pic);
                    thumbnail.compress(CompressFormat.PNG, 100, out);
                    out.flush();
                    out.close();
                }
            } catch (IOException e) {
                Log.e("BROKEN", "Could not write file " + e.getMessage());
            }   
    
        }  
    } 
    
    0 讨论(0)
  • 2020-12-09 07:36

    try this

            send.setOnClickListener(new View.OnClickListener() {
    
                @Override
    
                public void onClick(View arg0) {
                    Intent i = new Intent(Intent.ACTION_SEND);
                    i.putExtra(Intent.EXTRA_EMAIL, new String[]{"fake@fake.edu"});
                    i.putExtra(Intent.EXTRA_SUBJECT, "On The Job");
                    //Log.d("URI@!@#!#!@##!", Uri.fromFile(pic).toString() + "   " + pic.exists());
                    if(pic != null)
                    {  i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(pic));}
    
                    i.setType("image/png");
                    i.setType("message/rfc822");
    
                    startActivity(Intent.createChooser(i, "Share you on the jobing"));
    
                }
            });
        }
    
    0 讨论(0)
  • 2020-12-09 07:37

    try this

            Intent i = new Intent(Intent.ACTION_SEND);
        i.setType("text/plain");
        i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"123@gmail.com"});
        i.putExtra(Intent.EXTRA_SUBJECT, " report");
        i.putExtra(Intent.EXTRA_TEXT   , "PFA");
        i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(destinationFile));//pngFile 
    
            startActivity(Intent.createChooser(i, "Send mail..."));
    
    0 讨论(0)
提交回复
热议问题