1

我正在学习 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);
            }
        }
}
4

2 回答 2

0

因此,您想从 ImageView 中捕获图像,对其进行压缩然后附加。基本上这个过程是:

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

首先获取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);

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

于 2013-10-10T05:07:03.420 回答
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 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));
于 2013-10-10T04:49:50.970 回答