Android仅通过应用程序中的电子邮件发送1张图像

Android仅通过应用程序中的电子邮件发送1张图像,android,image,email,camera,Android,Image,Email,Camera,我已经在我的应用程序上创建了一个“联系我们”页面,你可以选择将图片发送到预先确定的电子邮件地址。我遇到的问题是从手机的多媒体资料中获取所有图像,然后通过电子邮件发送。我只想发一张照片。我似乎不知道要改变什么才能只发送一张图片。有人能帮忙吗 这是我的密码: public class EmailActivity extends Activity { Button send; EditText address, subject, emailtext; p

我已经在我的应用程序上创建了一个“联系我们”页面,你可以选择将图片发送到预先确定的电子邮件地址。我遇到的问题是从手机的多媒体资料中获取所有图像,然后通过电子邮件发送。我只想发一张照片。我似乎不知道要改变什么才能只发送一张图片。有人能帮忙吗

这是我的密码:

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.random.jbrefurb/quote");

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

                            Uri pngUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;


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


                                      emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{ "random@yahoo.co.uk"});

                                      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/jpeg");


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


                        }
                });

        Button back = (Button) findViewById(R.id.button1); 
        back.setOnClickListener(new View.OnClickListener() { 
            public void onClick(View view) {
                 // fire intent
                finish(); // finish current activity
                Intent austinIntent = new Intent(view.getContext(), 
                        ContactActivity.class); 
                startActivityForResult(austinIntent, 0);


            } 


        });




        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/jpeg");         
            Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);         
            OutputStream outstream;                 
            try {                         
                outstream = getContentResolver().openOutputStream(uri);          
                x.compress(Bitmap.CompressFormat.JPEG, 100, outstream);         
                outstream.close();                 
                } catch (FileNotFoundException e) {                         
                    //                 
                    }catch (IOException e){                         
                        //                 
                        }         
            } }   
    } 

非常感谢您这是我在应用程序中使用的代码如果您需要帮助,请尝试

 private OnClickListener shareemail=new OnClickListener(){
        @Override
        public void onClick(View v) {


            String address = "your emailaddress";
        File filee;
       if(address.length()==0)  
       {
           AlertDialog.Builder ab=new AlertDialog.Builder(null);
            ab.setMessage("Email Address must not be empty!");
            ab.setPositiveButton("OK", new DialogInterface.OnClickListener(){
               @Override 
               public void onClick(DialogInterface dialog, int which) {

               }
            });
            ab.show();
       }
       else
       {

          ArrayList<Uri> uris = new ArrayList<Uri>();
         Uri u;
         Intent emailSession = new Intent(Intent.ACTION_SEND_MULTIPLE);
         emailSession.putExtra(Intent.EXTRA_SUBJECT,"your subject");
         emailSession.setType("images/*");
         emailSession.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] {address});
         emailSession.putExtra(android.content.Intent.EXTRA_TEXT,"body text");
        FileWriter fw;
        BufferedWriter bw;
        try{

              filee = new File(path of image you want to send);
              if(filee.exists())
              {

                Uri u1 = Uri.fromFile(filee);
                uris.add(u1);
                emailSession.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);

             startActivity(emailSession);
            }
        }
            catch (ActivityNotFoundException e)
            {
                Toast.makeText(getBaseContext(),  e.getMessage(), Toast.LENGTH_SHORT).show();
            }
    }}
    };
private OnClickListener shareemail=new OnClickListener(){
@凌驾
公共void onClick(视图v){
String address=“您的电子邮件地址”;
文件档案;
if(address.length()==0)
{
AlertDialog.Builder ab=新建AlertDialog.Builder(空);
ab.setMessage(“电子邮件地址不得为空!”);
ab.setPositiveButton(“确定”,新的DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
}
});
ab.show();
}
其他的
{
ArrayList URI=新的ArrayList();
乌里乌;
Intent emailSession=新的意图(Intent.ACTION\u SEND\u MULTIPLE);
emailSession.putExtra(Intent.EXTRA_SUBJECT,“您的主题”);
emailSession.setType(“images/*”);
emailSession.putExtra(android.content.Intent.EXTRA_EMAIL,新字符串[]{address});
emailSession.putExtra(android.content.Intent.EXTRA_-TEXT,“body-TEXT”);
文件编写器;
缓冲写入器bw;
试一试{
filee=新文件(要发送的图像的路径);
if(filee.exists())
{
Uri u1=Uri.fromFile(filee);
uris.add(u1);
emailSession.putParcelableArrayListExtra(Intent.EXTRA\u流,URI);
startActivity(电子邮件会话);
}
}
捕获(ActivityNotFounde异常)
{
Toast.makeText(getBaseContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
}
}}
};