Pergunta

The problem sending the email from the gallery is working fine but not the captured image. I tried different codes some of them are commented here. Or just explain and teach me how it works.

This app takes date from the user and then send it using an email to a predefined email address, but the problem is the image can be attached in two ways one is getting the image from the gallery which is already captured and the other option is getting the user to capture an image and then uploading it and then sending it to the email as attachment.

This the second Activity in my App

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);

            }

        }

    }   


}`

And my xml file is ;

<?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>
Foi útil?

Solução

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 you have to send a captured image as attachment in email then you can try above code.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top