Android:如何从应用程序发送图像作为电子邮件附件?

发布于 2024-12-07 20:46:14 字数 5182 浏览 0 评论 0原文

我目前正在尝试创建一个应用程序,该应用程序可以拍摄照片,然后将该照片附加到一封电子邮件中,该电子邮件将发送到预先确定的电子邮件地址。

我可以使用电子邮件,也可以使用相机。我似乎无法获取相机拍摄的照片以添加为附件。我在应用程序中弹出了图像作为一种预览图像,如果这就是它无法附加的原因,我可以毫无问题地将其取出。

发送电子邮件时,图片已创建,但已损坏且无法打开。就好像我正在创建一张不存在的图片。我认为这将是将拍摄的照片绑定到创建附件部分的情况,但我不知道!如果有人可以帮助我将非常感激!

这是我的 MainActivity ,其中电子邮件与相机一起创建:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
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 EmailActivity extends Activity {
        Button send;
        EditText address, subject, emailtext;
        protected static final int CAMERA_PIC_REQUEST = 0;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.email);
        send=(Button) findViewById(R.id.emailsendbutton);
        address=(EditText) findViewById(R.id.emailaddress);
        subject=(EditText) findViewById(R.id.emailsubject);
        emailtext=(EditText) findViewById(R.id.emailtext);

        send.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                                // TODO Auto-generated method stub

                            if
                            (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
                            {

                            }

                            File pngDir = new File(

                                    Environment.getExternalStorageDirectory(),
                                    "Android/data/com.phstudios.jbrefurb/quote");

                            if (!pngDir.exists())
                                pngDir.mkdirs();

                            File pngFile = new File(pngDir, "pic1.png");
                            Uri pngUri = Uri.fromFile(pngFile);


                                     Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);

                                      emailIntent.setType("image/png");

                                      emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{ "[email protected]"});

                                      emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject.getText());

                                      emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, emailtext.getText());

                                      emailIntent.putExtra(android.content.Intent.EXTRA_STREAM, pngUri);

                                      emailIntent.setType("image/png");


                                    EmailActivity.this.startActivity(Intent.createChooser(emailIntent, "Send mail..."));

     }
            });

 Button camera = (Button) findViewById(R.id.button2); 
        camera.setOnClickListener(new View.OnClickListener() { 
            public void onClick(View view) {
                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                  startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);  
;

                }     
            });  
        }  

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {         
        if (requestCode== 0 && resultCode == Activity.RESULT_OK){                 
            Bitmap x = (Bitmap) data.getExtras().get("data");                 
            ((ImageView)findViewById(R.id.imageView1)).setImageBitmap(x);                 
            ContentValues values = new ContentValues();

            values.put(Images.Media.TITLE, "title");         
            values.put(Images.Media.BUCKET_ID, "test");         
            values.put(Images.Media.DESCRIPTION, "test Image taken");         
            values.put(Images.Media.MIME_TYPE, "image/png");         
            Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);         
            OutputStream outstream;                 
            try {                         
                outstream = getContentResolver().openOutputStream(uri);          
                x.compress(Bitmap.CompressFormat.JPEG, 70, outstream);         
                outstream.close();                 
                } catch (FileNotFoundException e) {                         
                    //                 
                    }catch (IOException e){                         
                        //                 
                        }         
            } }   
    } 

我希望它很简单,我只是不将它们链接在一起。

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 have the email working and I have the camera working. I cant seem to get the picture that the camera has taken to add as an attachment. I have the image popping up in the app as a kind of preview image which I have no problem taking out if this is why it wont attach.

When the email is sent a picture has been created but is corrupt and doesn't open. Its as if I'm creating a non existent picture. I think it will be a case of tying the picture taken to the creating attachment part but I have no idea! If anybody could help I would be extremely grateful!

Here is my MainActivity where the email is being created along with the camera:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
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 EmailActivity extends Activity {
        Button send;
        EditText address, subject, emailtext;
        protected static final int CAMERA_PIC_REQUEST = 0;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.email);
        send=(Button) findViewById(R.id.emailsendbutton);
        address=(EditText) findViewById(R.id.emailaddress);
        subject=(EditText) findViewById(R.id.emailsubject);
        emailtext=(EditText) findViewById(R.id.emailtext);

        send.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                                // TODO Auto-generated method stub

                            if
                            (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
                            {

                            }

                            File pngDir = new File(

                                    Environment.getExternalStorageDirectory(),
                                    "Android/data/com.phstudios.jbrefurb/quote");

                            if (!pngDir.exists())
                                pngDir.mkdirs();

                            File pngFile = new File(pngDir, "pic1.png");
                            Uri pngUri = Uri.fromFile(pngFile);


                                     Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);

                                      emailIntent.setType("image/png");

                                      emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{ "[email protected]"});

                                      emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject.getText());

                                      emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, emailtext.getText());

                                      emailIntent.putExtra(android.content.Intent.EXTRA_STREAM, pngUri);

                                      emailIntent.setType("image/png");


                                    EmailActivity.this.startActivity(Intent.createChooser(emailIntent, "Send mail..."));

     }
            });

 Button camera = (Button) findViewById(R.id.button2); 
        camera.setOnClickListener(new View.OnClickListener() { 
            public void onClick(View view) {
                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                  startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);  
;

                }     
            });  
        }  

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {         
        if (requestCode== 0 && resultCode == Activity.RESULT_OK){                 
            Bitmap x = (Bitmap) data.getExtras().get("data");                 
            ((ImageView)findViewById(R.id.imageView1)).setImageBitmap(x);                 
            ContentValues values = new ContentValues();

            values.put(Images.Media.TITLE, "title");         
            values.put(Images.Media.BUCKET_ID, "test");         
            values.put(Images.Media.DESCRIPTION, "test Image taken");         
            values.put(Images.Media.MIME_TYPE, "image/png");         
            Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);         
            OutputStream outstream;                 
            try {                         
                outstream = getContentResolver().openOutputStream(uri);          
                x.compress(Bitmap.CompressFormat.JPEG, 70, outstream);         
                outstream.close();                 
                } catch (FileNotFoundException e) {                         
                    //                 
                    }catch (IOException e){                         
                        //                 
                        }         
            } }   
    } 

I'm hoping its something simple that I'm just not linking them together.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

放手` 2024-12-14 20:46:14
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[]{"[email protected]"});
            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());
        }   
   
    }  
} 
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[]{"[email protected]"});
            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());
        }   
   
    }  
} 
我喜欢麦丽素 2024-12-14 20:46:14

试试这个

        Intent i = new Intent(Intent.ACTION_SEND);
    i.setType("text/plain");
    i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"[email protected]"});
    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..."));

try this

        Intent i = new Intent(Intent.ACTION_SEND);
    i.setType("text/plain");
    i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"[email protected]"});
    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..."));
小女人ら 2024-12-14 20:46:14

试试这个

        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[]{"[email protected]"});
                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"));

            }
        });
    }

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[]{"[email protected]"});
                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"));

            }
        });
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文