我想将捕获的图像作为附件发送到我的邮件中

时间:2014-02-21 08:50:49

标签: android eclipse image android-camera email-attachments

从图库发送电子邮件的问题工作正常但不是捕获的图像。我尝试了不同的代码,其中一些在这里评论。或者只是解释并教我如何运作。

此应用程序从用户获取日期,然后使用电子邮件将其发送到预定义的电子邮件地址,但问题是图像可以通过两种方式附加,一种是从已经捕获的图库中获取图像,另一种是选项是让用户捕获图像然后上传,然后将其作为附件发送到电子邮件。

这是我的应用程序中的第二个活动

package com.example.medipostrx;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class secondpage extends Activity{
    ImageView viewImage;
    File pic;
    Button b;
    Button c;

    String usernames;
    String clnumber;
    String clname;
    String spnnr;
    Uri imgui = null;
    int h = 0;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.secondpage);
        Bundle data = getIntent().getExtras();
         usernames = data.getString("user");
         clnumber=data.getString("clnu");
         clname=data.getString("clnam");
         spnnr = data.getString("spn");
        b=(Button)findViewById(R.id.btntake);
c= (Button) findViewById(R.id.btnsub);
final ImageView y = (ImageView) findViewById(R.id.viewImage);
c.setOnClickListener(new View.OnClickListener() {

    @Override

    public void onClick(View v) {

        Intent i = new Intent(Intent.ACTION_SEND);
        //i.setType("message/rfc822");
        i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"dohkzn1@medipost.co.za"});
        //i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"rushinbush@hotmail.com"});
        i.putExtra(Intent.EXTRA_SUBJECT, "Clinic Number: " + clnumber );
        i.putExtra(Intent.EXTRA_TEXT   , "Username: " + usernames + "\nClinic Number: " + clnumber + "\nClinic Name: " + clname + "\nBranch: " + spnnr);

        //if(h==1){
        if(imgui != null){
            i.putExtra(Intent.EXTRA_STREAM, imgui);
            i.setType("image/png");
        }else{
            i.setType("plain/text");
        }
        //}//else{
            /*Drawable d =y.getBackground();
            BitmapDrawable bitDw = ((BitmapDrawable) d);
            Bitmap bitmap = bitDw.getBitmap();
            File  mFile = savebitmap(bitmap);
            Uri u = null;
               u = Uri.fromFile(mFile);
               i.putExtra(Intent.EXTRA_STREAM, u);
               i.setType("image/png");*/
            //i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(pic));
            //i.setType("image/png");
             //Toast.makeText(secondpage.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();

        //}
        try {
            startActivity(Intent.createChooser(i, "Send mail..."));
        } catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(secondpage.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
        }

    }

});
        viewImage=(ImageView)findViewById(R.id.viewImage);

        b.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                selectImage();

            }

        });

    }
    private File savebitmap(Bitmap bmp) {
          String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
          OutputStream outStream = null;
         // String temp = null;
        File file = new File(extStorageDirectory, "temp.png");
          if (file.exists()) {
           file.delete();
           file = new File(extStorageDirectory, "temp.png");

          }

          try {
           outStream = new FileOutputStream(file);
           bmp.compress(Bitmap.CompressFormat.PNG, 100, outStream);
           outStream.flush();
           outStream.close();

          } catch (Exception e) {
           e.printStackTrace();
           return null;
          }
          return file;
         }


    @Override

    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds options to the action bar if it is present.

        getMenuInflater().inflate(R.menu.main, menu);

        return true;

    }



      private void selectImage() {



        final CharSequence[] options = { "Take Photo", "Choose from Gallery","Cancel" };



        AlertDialog.Builder builder = new AlertDialog.Builder(secondpage.this);

        builder.setTitle("Add Photo!");

        builder.setItems(options, new DialogInterface.OnClickListener() {

            @Override

            public void onClick(DialogInterface dialog, int item) {

                if (options[item].equals("Take Photo"))

                {

                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                    File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");

                    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));

                    startActivityForResult(intent, 1);


                }

                else if (options[item].equals("Choose from Gallery"))

                {

                    Intent intent = new   Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                    startActivityForResult(intent, 2);



                }

                else if (options[item].equals("Cancel")) {

                    dialog.dismiss();

                }

            }

        });

        builder.show();

    }



    @Override

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK) {

            if (requestCode == 1) {
                h=1;
                File f = new File(Environment.getExternalStorageDirectory().toString());

                for (File temp : f.listFiles()) {

                    if (temp.getName().equals("temp.jpg")) {

                        f = temp;
                        File photo = new File(Environment.getExternalStorageDirectory(), "temp.jpg");

                        break;

                    }

                }

                try {

                    Bitmap bitmap;

                    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();



                    bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(),

                            bitmapOptions); 



                    viewImage.setImageBitmap(bitmap);




                    String path = android.os.Environment

                            .getExternalStorageDirectory()

                            + File.separator

                            + "Phoenix" + File.separator + "default";

                    f.delete();

                    OutputStream outFile = null;

                    File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg");

                    try {

                        outFile = new FileOutputStream(file);

                        bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile);
//pic=file;
                        outFile.flush();

                        outFile.close();


                    } catch (FileNotFoundException e) {

                        e.printStackTrace();

                    } catch (IOException e) {

                        e.printStackTrace();

                    } catch (Exception e) {

                        e.printStackTrace();

                    }

                } catch (Exception e) {

                    e.printStackTrace();

                }

            } else if (requestCode == 2) {



                Uri selectedImage = data.getData();
imgui = selectedImage;
                String[] filePath = { MediaStore.Images.Media.DATA };

                Cursor c = getContentResolver().query(selectedImage,filePath, null, null, null);

                c.moveToFirst();

                int columnIndex = c.getColumnIndex(filePath[0]);

                String picturePath = c.getString(columnIndex);

                c.close();

                Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath));

                Log.w("path of image from gallery......******************.........", picturePath+"");

                viewImage.setImageBitmap(thumbnail);

            }

        }

    }   


}`

我的xml文件是;

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/filenumber" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/btntake"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/takepicutre" />
        <ImageView

            android:id="@+id/viewImage"

            android:layout_width="200dp"

            android:layout_height="200dp"
            android:layout_gravity="center"

            android:src="@drawable/cameraa" />

        <Button
            android:id="@+id/btnsub"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/email" />

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

Intent iShare;
iShare = new Intent(Intent.ACTION_SEND);
iShare.setType("image/png");
iShare.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+fn.getPath()));
iShare.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(new StringBuilder()
        .append("<p><b>Path</b></p>").append("<small><p>"+sb+"</p></small>").toString()));
startActivity(Intent.createChooser(iShare, "Share"));

AFAIK您必须将捕获的图像作为附件发送到电子邮件中,然后您可以尝试上面的代码。