将图像从相机附加到电子邮件 - android

时间:2014-09-29 04:18:51

标签: android email android-camera

我有一个拍照片的按钮,将缩略图存储在Imageview中,当用户点击另一个按钮时,它需要将该图像附加到电子邮件中。

我已经按照Android网站上的“简单拍照”说明,但没有附件。有人可以看到我出错的地方,以及我需要做些什么来解决它?

我的活动结果代码:

public void onActivityResult(int requestcode, int resultcode, Intent data) {
        if (requestcode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
            if (resultcode == RESULT_OK) {
                Bundle bundle = new Bundle();
                bundle = data.getExtras();
                Bitmap BMB;
                BMB = (Bitmap) bundle.get("data");
                Hoon_Image.setImageBitmap(BMB);
                try {
                    createImageFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                mDrawable = Hoon_Image.getDrawable();
                mBitmap = ((BitmapDrawable) mDrawable).getBitmap();
            }
        }
    }

private File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                storageDir      /* directory */
        );

        // Save a file: path for use with ACTION_VIEW intents
        mCurrentPhotoPath = "file:" + image.getAbsolutePath();
        return image;
    }

我的电子邮件意图:

if (Witness_response == "Yes"){
                    Intent emailIntent = new Intent(Intent.ACTION_SEND);
                    emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"roadsafety.app@shellharbour.nsw.gov.au"});
                    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Dob in a Hoon Report (Y)");
                    emailIntent.putExtra(Intent.EXTRA_TEXT, "Hoon report has been recieved " + emailBody);
                    emailIntent.putExtra(Intent.EXTRA_STREAM, mCurrentPhotoPath );
                    emailIntent.setType("message/rfc822");
                    startActivity(Intent.createChooser(emailIntent, "Choose email client..."));
                } else if (Witness_response == "No"){
                    Intent emailIntent = new Intent(Intent.ACTION_SEND);
                    emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"roadsafety.app@shellharbour.nsw.gov.au"});
                    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Dob in a Hoon Report (N)");
                    emailIntent.putExtra(Intent.EXTRA_TEXT, "Hoon report has been recieved " + emailBody);
                    emailIntent.putExtra(Intent.EXTRA_STREAM, mCurrentPhotoPath);
                    emailIntent.setType("message/rfc822");
                    startActivity(Intent.createChooser(emailIntent, "Choose email client..."));
                }

2 个答案:

答案 0 :(得分:1)

试试这段代码

  public class MainActivity extends Activity {
  private static final int CAMERA_REQUEST = 1888; 
private ImageView imageView;
private File f;
public File getAlbumDir()
{

    File storageDir = new File(
            Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES
            ), 
            "BAC/"
        ); 
     // Create directories if needed
    if (!storageDir.exists()) {
        storageDir.mkdirs();
    }

    return storageDir;
}
private File createImageFile() throws IOException {
    // Create an image file name

    String imageFileName =getAlbumDir().toString() +"/image.jpg";
    File image = new File(imageFileName);
    return image;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    this.imageView = (ImageView)this.findViewById(R.id.imageView1);
    Button photoButton = (Button) this.findViewById(R.id.button1);
    photoButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            try {
                f = createImageFile();
                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
                startActivityForResult(cameraIntent, CAMERA_REQUEST); 
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }



        }
    });
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {  

        Bitmap photo = BitmapFactory.decodeFile(f.getAbsolutePath());
        imageView.setImageBitmap(photo);

        Intent i = new Intent(Intent.ACTION_SEND);
        i.setType("message/rfc822");
        i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"email id"});
        i.putExtra(Intent.EXTRA_SUBJECT, "subject");
        i.putExtra(Intent.EXTRA_TEXT   , "body of email");

        Uri uri = Uri.fromFile(f);
        i.putExtra(Intent.EXTRA_STREAM, uri);
        try {
            startActivity(Intent.createChooser(i, "Send mail..."));
        } catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(MainActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
        }
    }  
} 

}

答案 1 :(得分:0)

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

emailIntent.setType("image/*");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"me@gmail.com"}); 
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Test Subject"); 
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,"go on read the emails");     
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(emailIntent, "Send Mail"));;

在这里,

uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), FILENAME));