在Email-android中将ImageView作为附件发送

时间:2013-10-10 04:38:40

标签: android email-attachments

我正在学习android并正在开发一个应用程序来拍照并通过电子邮件发送它们。我在下面的代码中已经在ImageView中获得了图片,但我不确定如何将此图片作为电子邮件附件发送,而不将图片保存到设备上的文件。

理想情况下,我想知道这是否可行?如果是,你可以指出我如何实现相同的正确方向。此外(可选)如果图片可以压缩。

public class EmailPic extends Activity implements OnClickListener{

       ImageButton ibEmail;
       Button bEmail;
       ImageView ivEmail;
       Intent intentEmail;
       Bitmap bmpEmail;
       final static int picData = 0;

        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.pic_email);
            initializeVars();
            InputStream is = getResources().openRawResource(R.drawable.ic_launcher);
            bmpEmail = BitmapFactory.decodeStream(is);

        }
        private void initializeVars() {
            ibEmail = (ImageButton)findViewById(R.id.ibTakePicEmail) ;
            ivEmail = (ImageView)findViewById(R.id.ivPicEmail);
            bEmail  = (Button) findViewById(R.id.bSendPicEmail);
            bEmail.setOnClickListener(this);
            ibEmail.setOnClickListener(this);
        }
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.ibTakePicEmail:
                intentEmail = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intentEmail,picData);
                break;

            case R.id.bSendPicEmail:

                String message = "email Body";                  
                String[] recipients = new String[]{"mymail.com"};
                Intent emailIntent  = new Intent(android.content.Intent.ACTION_SEND);
                emailIntent.setType("application/image");
                emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,recipients);
                emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "My Subject");
                emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,message);
                startActivity(emailIntent);
                break;
            }
        }
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {

            super.onActivityResult(requestCode, resultCode, data);
            if(resultCode == RESULT_OK){
                Bundle ext = data.getExtras();
                bmpEmail = (Bitmap)ext.get("data");
                //Log.d("StatusActivity","bmpEmail >>"+bmpEmail);
                ivEmail.setImageBitmap(bmpEmail);
            }
        }
}

2 个答案:

答案 0 :(得分:0)

试试如下:

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("image/*");
Uri uri = Uri.parse("android.resource://your package name/"+R.drawable.ic_launcher);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.putExtra(android.content.Intent.EXTRA_EMAIL,recipients);
shareIntent.putExtra(Intent.EXTRA_TEXT, message);
startActivity(Intent.createChooser(shareIntent, "Send your image"));

<强>编辑:

声明File变量

         File pic;

OnActivityResult()应用更改中,如下所示:

Bundle ext = data.getExtras();
    bmpEmail = (Bitmap)ext.get("data");
    try {
        File root = Environment.getExternalStorageDirectory();
        if (root.canWrite()){
             pic = new File(root, "pic.png");
            FileOutputStream out = new FileOutputStream(pic);
            bmpEmail.compress(CompressFormat.PNG, 100, out);
            out.flush();
            out.close();
        }
    } catch (IOException e) {
        Log.e("BROKEN", "Could not write file " + e.getMessage());
    }   

在您的发送电子邮件地址中添加

      emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(pic));

答案 1 :(得分:0)

因此,您希望从ImageView捕获图像,压缩它然后附加。 基本上这个过程是:

  1. 从ImageView获取图像。
  2. 转换为位图。
  3. 保存。(无论如何,如果要附加它,都必须这样做)
  4. 希望您之后可以删除它。
  5. 附加到电子邮件意图。
  6. 首先获取ImageView的缓存位图

    Bitmap myBitmap = yourImageView.getDrawingCache();
    

    这将从ImageView返回缓存的位图。 压缩并保存

    URI FILENAME;    //URI For the ImageView, we need earlier to send
    FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_WORLD_READABLE);
    myBitmap.compress(Bitmap.CompressFormat.PNG, 0, fos);
    fos.close();
    

    创建发送意图

                String message = "email Body";                  
                String[] recipients = new String[]{"mymail.com"};
                Intent emailIntent  = new Intent(android.content.Intent.ACTION_SEND);
                emailIntent.setType("application/image");
                emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,recipients);
                emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "My Subject");
                emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,message);
                emailIntent.putExtra(Intent.EXTRA_STREAM, FILENAME);     //this line is added to your code
                startActivity(emailIntent);
    

    如果需要,您可以在以后删除图像。