【问题标题】:Attach Image to an Email using onActivityResult使用 onActivityResult 将图像附加到电子邮件
【发布时间】:2014-02-04 08:13:14
【问题描述】:

之前我从图库中选择图片并附加到电子邮件中,请参阅here

但这次我允许用户使用内置摄像头捕捉图像,然后想将捕捉到的图像附加到电子邮件中......

我已经为两者编写了代码,用于捕获图像和发送电子邮件,请参阅下面的代码: 公共类 CameraActivity 扩展 Activity 实现 MediaScannerConnectionClient {

  private File myImageFile = null;
  private Uri myPicture = null;
  private MediaScannerConnection conn = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);

        ImageButton buttonCaptureImage = (ImageButton) findViewById(R.id.btnCaptureImage);
        buttonCaptureImage.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                captureImage();
            }
        });
    }

    public void captureImage()
    {
        myImageFile = new File(Environment.getExternalStorageDirectory()+"/CapturedImage.jpg");
        myImageFile.delete();
        myPicture = Uri.fromFile(myImageFile);
        Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        i.putExtra(MediaStore.EXTRA_OUTPUT, myPicture);
        startActivity(i);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode==0 && resultCode==Activity.RESULT_OK)
        {
          startScan();
        }
    }


 // to attach an image to email
    public void sendImage() {
        // TODO Auto-generated method stub
        Intent i = new Intent(Intent.ACTION_SEND);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        i.setType("message/rfc822");
        i.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/CapturedImage.jpg"));
        i.putExtra(Intent.EXTRA_EMAIL, new String[]{"recepient@mail.cin"});
        i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
        i.putExtra(Intent.EXTRA_TEXT, "body of email");
        startActivity(Intent.createChooser(i,"Send email via:"));   
    }
    private void startScan()
    {
        if(conn !=null)
        {
            conn.disconnect();
        }

        if(myImageFile.isFile()) {
            conn = new MediaScannerConnection(this, this);
            conn.connect();
        }
        else {
            Toast.makeText(this,
                "Image does not exist ?",
                Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onMediaScannerConnected() {
        conn.scanFile(myImageFile.getPath(), null);
    }

    @Override
    public void onScanCompleted(String path, Uri uri) {
        try {
            if (uri != null) {
                sendImage();
            }
        } finally {
            conn.disconnect();
            conn = null;
        } 
    }

要在 onActivityResult(...) 中将图像附加到电子邮件中,我在 onScanCompleted(...) 中使用了以下方法,但仍未解决

   // to attach an image to email
    public void sendImage() {
        // TODO Auto-generated method stub
        Intent i = new Intent(Intent.ACTION_SEND);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        i.setType("message/rfc822");
        i.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/CapturedImage.jpg"));
        i.putExtra(Intent.EXTRA_EMAIL, new String[]{"recepient@mail.cin"});
        i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
        i.putExtra(Intent.EXTRA_TEXT, "body of email");
        startActivity(Intent.createChooser(i,"Send email via:"));   
    }

【问题讨论】:

  • 您是否遇到任何错误,如果出现请从您的 logcat 发布堆栈跟踪?
  • 不,我只是想知道,如何将图像附加到电子邮件中,就在拍摄之后......
  • @SankarGanesh 我现在编辑了我的问题检查
  • 需要将sendImage的代码放在onScanCompleted中

标签: android email android-intent


【解决方案1】:

你需要像这样将 sendImage 的代码放在 onScanCompleted 中:

@Override
    public void onScanCompleted(String path, Uri uri) {
        try {
            if (uri != null) {
                sendImage();
            }
        } finally {
            conn.disconnect();
            conn = null;
        } 
    }

为了更好的理解,请看here

【讨论】:

    【解决方案2】:

    将图片附加到电子邮件

    Intent emailIntent = new Intent(Intent.ACTION_SEND);
    emailIntent.setType("text/html");
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "File attached");
    Uri uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "image_Path"));
    emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
    startActivity(Intent.createChooser(emailIntent, "Send mail..."));
    

    【讨论】:

      【解决方案3】:

      当我想与文本共享图像时,我设置了内容类型 image/*,并且我从文件描述符而不是从路径字符串创建 Uri 对象。

      Uri imageFileUri = Uri.fromFile(imageFile); //file descriptor should be valid
      
      Intent shareIntent = new Intent(Intent.ACTION_SEND);
      shareIntent.setType("image/*");
      shareIntent.putExtra(Intent.EXTRA_TEXT,"Some text");
      shareIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"recepient@mail.cin"});
      shareIntent.putExtra(Intent.EXTRA_STREAM, imageFileUri);
      shareIntent.putExtra(Intent.EXTRA_SUBJECT,"Subject");
      startActivity(shareIntent);
      

      它总是对我有用

      【讨论】:

        猜你喜欢
        • 2010-10-06
        • 2016-12-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-01
        • 2012-02-28
        相关资源
        最近更新 更多