Android的:如何将图像发送作为电子邮件附件的应用程序? [英] Android: How to send an image as email attachment from application?

查看:296
本文介绍了Android的:如何将图像发送作为电子邮件附件的应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在试图创建一个应用程序,将拍摄一张照片,然后将该图片附加到将去到pre电子邮件确定电子邮件地址。

我的邮件工作,我有摄像头的工作。我不能似乎得到相机已经采取添加为附件的图片。我的形象突然出现在应用程序作为一种preVIEW形象,我有没有问题取出,如果这就是为什么它不会重视。

在发送电子邮件已经创造了一个图片,但已损坏,无法打开。它好像我创建一个不存在的画面。我认为这将是捆绑带到创建连接部位的图像的情况下,但我不知道!如果有人可以帮助我将非常感谢!

下面是我的 MainActivity ,其中电子邮件正在随着相机创建的:

 进口的java.io.File;
进口java.io.FileNotFoundException;
进口java.io.IOException异常;
进口java.io.OutputStream中;

进口android.app.Activity;
进口android.content.ContentValues​​;
进口android.content.Intent;
进口android.graphics.Bitmap;
进口android.net.Uri;
进口android.os.Bundle;
进口android.os.Environment;
进口android.provider.MediaStore.Images;
进口android.provider.MediaStore.Images.Media;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.ImageView;

公共类EmailActivity延伸活动{
        按钮发送;
        的EditText地址,主题,emailtext;
        受保护的静态最终诠释CAMERA_PIC_REQUEST = 0;



    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.email);
        发送=(按钮)findViewById(R.id.emailsendbutton);
        地址=(EditText上)findViewById(R.id.emailaddress);
        主题=(EditText上)findViewById(R.id.emailsubject);
        emailtext =(EditText上)findViewById(R.id.emailtext);

        send.setOnClickListener(新OnClickListener(){

                        @覆盖
                        公共无效的onClick(视图v){
                                // TODO自动生成方法存根

                            如果
                            (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
                            {

                            }

                            文件pngDir =新的文件(

                                    Environment.getExternalStorageDirectory(),
                                    安卓/数据/ com.phstudios.jbrefurb /报价);

                            如果(!pngDir.exists())
                                pngDir.mkdirs();

                            文件pngFile =新的文件(pngDir,pic1.png);
                            乌里pngUri = Uri.fromFile(pngFile);


                                     意图emailIntent =新的意图(android.content.Intent.ACTION_SEND);

                                      emailIntent.setType(图像/ PNG);

                                      emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,新的String [] {random@yahoo.co.uk});

                                      emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,subject.getText());

                                      emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,emailtext.getText());

                                      emailIntent.putExtra(android.content.Intent.EXTRA_STREAM,pngUri);

                                      emailIntent.setType(图像/ PNG);


                                    EmailActivity.this.startActivity(Intent.createChooser(emailIntent,发送邮件...));

     }
            });

 按钮相机=(按钮)findViewById(R.id.button2);
        camera.setOnClickListener(新View.OnClickListener(){
            公共无效的onClick(视图查看){
                意图cameraIntent =新的意图(android.provider.MediaStore.ACTION_IM​​AGE_CAPTURE);
                  startActivityForResult(cameraIntent,CAMERA_PIC_REQUEST);
;

                }
            });
        }

    @覆盖
    保护无效onActivityResult(INT申请code,INT结果code,意图数据){
        如果(要求code == 0安培;&安培;结果code == Activity.RESULT_OK){
            。位图X =(位图)data.getExtras()获得(数据);
            ((ImageView的)findViewById(R.id.imageView1))setImageBitmap(X)。
            ContentValues​​值=新ContentValues​​();

            values​​.put(Images.Media.TITLE,标题);
            values​​.put(Images.Media.BUCKET_ID,测试);
            values​​.put(Images.Media.DESCRIPTION,测试图像拍摄);
            values​​.put(Images.Media.MIME_TYPE,图像/ PNG);
            。URI URI = getContentResolver()插入(Media.EXTERNAL_CONTENT_URI,价值观);
            的OutputStream outstream;
            尝试 {
                outstream = getContentResolver()openOutputStream(URI)。
                x.com preSS(Bitmap.Com pressFormat.JPEG,70,outstream);
                outstream.close();
                }赶上(FileNotFoundException异常E){
                    //
                    }赶上(IOException异常E){
                        //
                        }
            }}
    }
 

我希望它简单的东西,我只是没有把它们连接在一起。

解决方案

 进口android.os.Bundle;
进口android.app.Activity;
进口android.util.Log;
进口android.view.Menu;
进口android.view.MenuItem;
进口android.support.v4.app.NavUtils;

进口的java.io.File;
进口java.io.FileNotFoundException;
进口java.io.FileOutputStream中;
进口java.io.IOException异常;
进口java.io.OutputStream中;


进口android.content.ContentValues​​;
进口android.content.Intent;
进口android.graphics.Bitmap;
进口android.graphics.Bitmap.Com pressFormat;
进口android.net.Uri;

进口android.provider.MediaStore.Images;
进口android.provider.MediaStore.Images.Media;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.ImageView;


 公共类MainActivity延伸活动{
  按钮发送;
  位图的缩略图;
  文件PIC;
  的EditText地址,主题,emailtext;
  受保护的静态最终诠释CAMERA_PIC_REQUEST = 0;
@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    发送=(按钮)findViewById(R.id.emailsendbutton);
    地址=(EditText上)findViewById(R.id.emailaddress);
    主题=(EditText上)findViewById(R.id.emailsubject);
    emailtext =(EditText上)findViewById(R.id.emailtext);










    按钮相机=(按钮)findViewById(R.id.button1);
    camera.setOnClickListener(新View.OnClickListener(){

        @覆盖
        公共无效的onClick(查看为arg0){
            意图cameraIntent =新的意图(android.provider.MediaStore.ACTION_IM​​AGE_CAPTURE);
            startActivityForResult(cameraIntent,CAMERA_PIC_REQUEST);

        }
        });

        send.setOnClickListener(新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分享你);
        }
        });


}

保护无效onActivityResult(INT申请code,INT结果code,意图数据){
    如果(要求code == CAMERA_PIC_REQUEST){
     。缩略图=(位图)data.getExtras()获得(数据);
    ImageView的形象=(ImageView的)findViewById(R.id.imageView1);
    image.setImageBitmap(缩略图);


        尝试 {
            文件根= Environment.getExternalStorageDirectory();
            如果(root.canWrite()){
                 PIC =新的文件(根,pic.png);
                FileOutputStream中出=新的FileOutputStream(PIC);
                thumbnail.com preSS(比较pressFormat.PNG,100,出);
                了out.flush();
                out.close();
            }
        }赶上(IOException异常E){
            Log.e(破,无法写入文件+ e.getMessage());
        }

    }
}
 

I'm currently trying to create an app that will take a picture and then attach that picture to an email that will be going to a pre determined email address.

I have the email working and I have the camera working. I cant seem to get the picture that the camera has taken to add as an attachment. I have the image popping up in the app as a kind of preview image which I have no problem taking out if this is why it wont attach.

When the email is sent a picture has been created but is corrupt and doesn't open. Its as if I'm creating a non existent picture. I think it will be a case of tying the picture taken to the creating attachment part but I have no idea! If anybody could help I would be extremely grateful!

Here is my MainActivity where the email is being created along with the camera:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore.Images;
import android.provider.MediaStore.Images.Media;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

public class EmailActivity extends Activity {
        Button send;
        EditText address, subject, emailtext;
        protected static final int CAMERA_PIC_REQUEST = 0;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.email);
        send=(Button) findViewById(R.id.emailsendbutton);
        address=(EditText) findViewById(R.id.emailaddress);
        subject=(EditText) findViewById(R.id.emailsubject);
        emailtext=(EditText) findViewById(R.id.emailtext);

        send.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                                // TODO Auto-generated method stub

                            if
                            (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
                            {

                            }

                            File pngDir = new File(

                                    Environment.getExternalStorageDirectory(),
                                    "Android/data/com.phstudios.jbrefurb/quote");

                            if (!pngDir.exists())
                                pngDir.mkdirs();

                            File pngFile = new File(pngDir, "pic1.png");
                            Uri pngUri = Uri.fromFile(pngFile);


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

                                      emailIntent.setType("image/png");

                                      emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{ "random@yahoo.co.uk"});

                                      emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject.getText());

                                      emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, emailtext.getText());

                                      emailIntent.putExtra(android.content.Intent.EXTRA_STREAM, pngUri);

                                      emailIntent.setType("image/png");


                                    EmailActivity.this.startActivity(Intent.createChooser(emailIntent, "Send mail..."));

     }
            });

 Button camera = (Button) findViewById(R.id.button2); 
        camera.setOnClickListener(new View.OnClickListener() { 
            public void onClick(View view) {
                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                  startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);  
;

                }     
            });  
        }  

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {         
        if (requestCode== 0 && resultCode == Activity.RESULT_OK){                 
            Bitmap x = (Bitmap) data.getExtras().get("data");                 
            ((ImageView)findViewById(R.id.imageView1)).setImageBitmap(x);                 
            ContentValues values = new ContentValues();

            values.put(Images.Media.TITLE, "title");         
            values.put(Images.Media.BUCKET_ID, "test");         
            values.put(Images.Media.DESCRIPTION, "test Image taken");         
            values.put(Images.Media.MIME_TYPE, "image/png");         
            Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);         
            OutputStream outstream;                 
            try {                         
                outstream = getContentResolver().openOutputStream(uri);          
                x.compress(Bitmap.CompressFormat.JPEG, 70, outstream);         
                outstream.close();                 
                } catch (FileNotFoundException e) {                         
                    //                 
                    }catch (IOException e){                         
                        //                 
                        }         
            } }   
    } 

I'm hoping its something simple that I'm just not linking them together.

解决方案

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;


import android.content.ContentValues;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.net.Uri;

import android.provider.MediaStore.Images;
import android.provider.MediaStore.Images.Media;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;


 public class MainActivity extends Activity {
  Button send;
  Bitmap thumbnail;
  File pic;
  EditText address, subject, emailtext;
  protected static final int CAMERA_PIC_REQUEST = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    send=(Button) findViewById(R.id.emailsendbutton);
    address=(EditText) findViewById(R.id.emailaddress);
    subject=(EditText) findViewById(R.id.emailsubject);
    emailtext=(EditText) findViewById(R.id.emailtext);










    Button camera = (Button) findViewById(R.id.button1); 
    camera.setOnClickListener(new View.OnClickListener() {

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

        }
        });

        send.setOnClickListener(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"));
        }
        });


} 

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.imageView1);  
    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());
        }   

    }  
} 

这篇关于Android的:如何将图像发送作为电子邮件附件的应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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