将影像附加到电子邮件 [英] Attaching an image to an email

查看:124
本文介绍了将影像附加到电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

写一个应用程序,它需要从摄像机的图像,并检索其为位图。然后,我将此文件写入磁盘并尝试发送照片作为电子邮件,在Gmail应用它说有但是附件时我没有收到文件附加在电子邮件。下面是我想获得工作的两个相应的方法。

 保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
    如果(要求code == CAMERA_PIC_REQUEST){
    。缩略图=(位图)data.getExtras()获得(数据);
    ImageView的图像=(ImageView的)findViewById(R.id.photoResultView);
    image.setImageBitmap(缩略图);
    尝试{
            PIC =新的文件(PIC);        FileOutputStream中出=新的FileOutputStream(PIC);
        thumbnail.com preSS(比较pressFormat.PNG,100,出);
        了out.flush();
        out.close();    }
    赶上(java.io.FileNotFoundException E){
        //没关系,我们可能还没有创建它尚未
    }
    捕获(的Throwable t)的{
        烤面包
            .makeText(这一点,异常:+ t.toString(),Toast.LENGTH_LONG)
            。显示();    }
    sendPictureButton.setVisibility(Button.VISIBLE);
    }
}
私人OnClickListener sendPictureButtonListener =新OnClickListener(){    @覆盖
    公共无效的onClick(查看为arg0){        意图I =新意图(Intent.ACTION_SEND);
        i.putExtra(Intent.EXTRA_EMAIL,新的String [] {fakeaddress@hello.edu});
        i.putExtra(Intent.EXTRA_SUBJECT,在职);
        i.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(PIC));        i.setType(图像/ PNG);
        startActivity(Intent.createChooser(我)关于jobing分享你的);    }
    };


解决方案

首先需要访问的SD卡,然后找到它,并写信给它,并抓住URI的文件。

下面是功能code:

 保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
    如果(要求code == CAMERA_PIC_REQUEST){
    。缩略图=(位图)data.getExtras()获得(数据);
    ImageView的图像=(ImageView的)findViewById(R.id.photoResultView);
    image.setImageBitmap(缩略图);
        尝试{
            文件根= Environment.getExternalStorageDirectory();
            如果(root.canWrite()){
                PIC =新的文件(根,pic.png);
                FileOutputStream中出=新的FileOutputStream(PIC);
                thumbnail.com preSS(比较pressFormat.PNG,100,出);
                了out.flush();
                out.close();
            }
        }赶上(IOException异常五){
            Log.e(破,无法写入文件+ e.getMessage());
        }
    sendPictureButton.setVisibility(Button.VISIBLE);
    }
}
私人OnClickListener takePictureButtonListener =新OnClickListener(){    @覆盖
    公共无效的onClick(查看为arg0){
        意图cameraIntent =新意图(android.provider.MediaStore.ACTION_IM​​AGE_CAPTURE);
        startActivityForResult(cameraIntent,CAMERA_PIC_REQUEST);    }
    };    私人OnClickListener sendPictureButtonListener =新OnClickListener(){    @覆盖
    公共无效的onClick(查看为arg0){        意图I =新意图(Intent.ACTION_SEND);
        i.putExtra(Intent.EXTRA_EMAIL,新的String [] {fake@fake.edu});
        i.putExtra(Intent.EXTRA_SUBJECT,在职);
        //Log.d(\"URI@!@#!#!@##,Uri.fromFile(PIC)的ToString()++ pic.exists())!;
        i.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(PIC));        i.setType(图像/ PNG);
        startActivity(Intent.createChooser(我)关于jobing分享你的);
    }
    };

Writing an app which takes a picture from the camera and retrieves it as a bitmap. I then write this file to the disk and attempt to send the picture as an email, in the gmail app it says there is an attachment however when I receive the email no file is attached. Here are the two appropriate methods I am trying to get working.

protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    if (requestCode == CAMERA_PIC_REQUEST) {  
    thumbnail = (Bitmap) data.getExtras().get("data");  
    ImageView image = (ImageView) findViewById(R.id.photoResultView);  
    image.setImageBitmap(thumbnail);


    try {
            pic = new File("pic");

        FileOutputStream out = new FileOutputStream(pic);
        thumbnail.compress(CompressFormat.PNG, 100, out);
        out.flush();
        out.close();

    }
    catch (java.io.FileNotFoundException e) {
        // that's OK, we probably haven't created it yet
    }
    catch (Throwable t) {
        Toast
            .makeText(this, "Exception: "+t.toString(), Toast.LENGTH_LONG)
            .show();

    }   
    sendPictureButton.setVisibility(Button.VISIBLE);
    }  
}
private OnClickListener sendPictureButtonListener = new OnClickListener() {

    @Override
    public void onClick(View arg0){

        Intent i = new Intent(Intent.ACTION_SEND);
        i.putExtra(Intent.EXTRA_EMAIL, new String[]{"fakeaddress@hello.edu"});
        i.putExtra(Intent.EXTRA_SUBJECT,"On The Job");
        i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(pic));

        i.setType("image/png");
        startActivity(Intent.createChooser(i,"Share you on the jobing"));

    }
    };

解决方案

First needed to access the SD Card, then find it and write to it and grab the URI of the file.

Here is the functioning code:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    if (requestCode == CAMERA_PIC_REQUEST) {  
    thumbnail = (Bitmap) data.getExtras().get("data");  
    ImageView image = (ImageView) findViewById(R.id.photoResultView);  
    image.setImageBitmap(thumbnail);


        try {
            File root = Environment.getExternalStorageDirectory();
            if (root.canWrite()){
                pic = new File(root, "pic.png");
                FileOutputStream out = new FileOutputStream(pic);
                thumbnail.compress(CompressFormat.PNG, 100, out);
                out.flush();
                out.close();
            }
        } catch (IOException e) {
            Log.e("BROKEN", "Could not write file " + e.getMessage());
        }   
    sendPictureButton.setVisibility(Button.VISIBLE);
    }  
}  


private OnClickListener takePictureButtonListener = new OnClickListener() {

    @Override
    public void onClick(View arg0){
        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);  

    }
    };

    private OnClickListener sendPictureButtonListener = new OnClickListener() {

    @Override
    public void onClick(View arg0){

        Intent i = new Intent(Intent.ACTION_SEND);
        i.putExtra(Intent.EXTRA_EMAIL, new String[]{"fake@fake.edu"});
        i.putExtra(Intent.EXTRA_SUBJECT,"On The Job");
        //Log.d("URI@!@#!#!@##!", Uri.fromFile(pic).toString() + "   " + pic.exists());
        i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(pic));

        i.setType("image/png");
        startActivity(Intent.createChooser(i,"Share you on the jobing"));
    }
    };

这篇关于将影像附加到电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆