Android App to Send Images Via Email

2019-04-14 11:32发布

问题:

Ok once again, I am in way over my head here while learning android. After finally developing my simple little app, I am trying to use some of the benefits of having a native app.

So, project one, make a page which can send images via email (either from the gallery or camera)

Essentially its a select and send via email, but I don't even know where to start.

I found some code that somebody else was asking about at; Android App Take/ Email Photo

I tried this, but get all sorts of errors from eclipse, replating to downloadedPic section.

If somebody could please take a look and advise me on the best way to do this, that would be amazing. As usual sorry for my beginers stupidity, but I guess everyone has to learn somewhere

THIS IS MY .JAVA AT PRESENT

public class Photos extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_photos);
    getActionBar().setDisplayHomeAsUpEnabled(true);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_photos, menu);
    return true;
}

THIS IS MY .XML AT PRESENT

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Please chose the options below to select and upload photos into the 
    DCC for the selected project..."
    tools:context=".Photos"
    android:textAppearance="?android:attr/textAppearanceMedium" />


</LinearLayout>

回答1:

First what should you do is get the image storage path by using the file,

 File *photo = new File(Environment.getExternalStorageDirectory()+"/Android/data/"+getApplicationContext().getPackageName()+"/Fault", imagename+".png");

Then Convert that file path in to Uri

Uri imageuri = Uri.fromFile(photo); 

Finally send it the image via email using your imageuri

Intent send_report = new Intent(Intent.ACTION_SEND);
                                        send_report.putExtra(Intent.EXTRA_EMAIL, new String[]{ email_emailid}); 
                                        send_report.putExtra(Intent.EXTRA_SUBJECT, email_subject);
                                        send_report.putExtra(Intent.EXTRA_STREAM, imageuri);
                                        send_report.putExtra(Intent.EXTRA_TEXT, email_body);  
                                        send_report.setType("text/plain");
                                        send_report.setType("image/png");
                                        startActivityForResult(Intent.createChooser(send_report, "Choose an Email client"), 77);

Hope it helps.



回答2:

Get Your Image First :

 // Get Image form mnt/sdcard/YOUR_FLODER/my_image.png    
    ImageView my_Image = (ImageView)findViewById(R.id.my_Image);    
    Imagepath="/sdcard/YOUR_FLODER/"+my_iamge+".png";
     bitmap = BitmapFactory.decodeFile(Imagepath);

Fetch mail address:

// This Will fetch merchant's Email id from Deivce.
    Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+

    AccountManager manager =(AccountManager)getSystemService(ACCOUNT_SERVICE);
    //Account[] accounts = AccountManager.get(getApplicationContext()).getAccounts();
    Account[] accounts = manager.getAccounts();

    for (Account account : accounts) 
    {
        if (emailPattern.matcher(account.name).matches()) 
        {
             possibleEmail = account.name;
        }
    }

Send Click Event :

        Intent i = new Intent(android.content.Intent.ACTION_SEND);
        i.setType("image/png");
        i.putExtra(Intent.EXTRA_CC,new String[]{possibleEmail});  
        i.putExtra(android.content.Intent.EXTRA_SUBJECT, "Mail With Image attachment");
        startActivity(Intent.createChooser(i2, "Send Email..."));

At The End Photos.java

public class Photos extends Activity
{
     @Override
public void onCreate(Bundle savedInstanceState) 
{
     super.onCreate(savedInstanceState);
    setContentView(R.layout.Activity_Photos);

          // your image fetching code
          // fetch mail code 
          // write button click event 
               // put intent code in click event

    }
}

So Hope Now you Get Full Code.