拍照,减小尺寸并通过电子邮件发送到android

时间:2014-06-30 13:13:27

标签: android email photo

我已经看到有两种方式以编程方式发送电子邮件。

1-致电意向发送电子邮件 要么 2- Javamail API

我尝试使用第二种方式,(Sending Email in Android using JavaMail API without using the default/built-in app

但是现在我需要先拍照并在发送包含此图片的电子邮件后,最好发送此尺寸缩小的图片。

可以帮我吗?使用javamail api是更好的解决方案吗?或者我怎么做到这一点?

1 个答案:

答案 0 :(得分:2)

使用此意图拍照

                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                File f = new File(android.os.Environment.getExternalStorageDirectory(), AppInfo.getInstance().getCurrentLoginUserInfo().getId()+".jpg");
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
                intent.putExtra("return-data", true);
                startActivityForResult(intent, 1);

以及您的活动结果.... 注意Bitmap bitmap = getScaledBitmap(uri.getPath(), 200, true); 200是您的最大图片尺寸。

if(requestCode == 1)
        {
String base = Environment.getExternalStorageDirectory().getAbsolutePath().toString();
            final String imgPath = base + "/" +AppInfo.getInstance().getCurrentLoginUserInfo().getId()+".jpg";
            File file = new File(imgPath);
            if (file.exists()) 
            {
                Uri uri = Uri.fromFile(file);

                Log.d(TAG, "Image Uri path: " + uri.getPath());

                Bitmap bitmap = getScaledBitmap(uri.getPath(), 200, true);

            }}

此方法在调整大小后返回图像位图 -

private Bitmap getScaledBitmap(String imagePath, float maxImageSize, boolean filter) {

    FileInputStream in;
    BufferedInputStream buf;

    try {
        in = new FileInputStream(imagePath);

        buf = new BufferedInputStream(in);
        Bitmap realImage = BitmapFactory.decodeStream(buf);


        float ratio = Math.min(
                (float) maxImageSize / realImage.getWidth(),
                (float) maxImageSize / realImage.getHeight());

        int width = Math.round((float) ratio * realImage.getWidth());
        int height = Math.round((float) ratio * realImage.getHeight());

        Bitmap newBitmap = Bitmap.createScaledBitmap(realImage, width, height, filter);
        return newBitmap;

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return null;
}

现在您已经缩放了位图图像,您可以将其附加到邮件中 -

请阅读此内容 - http://www.tutorialsbuzz.com/2014/02/send-mail-attachment-android-application.html

希望这会帮助你。