Hello friends, Today i am going to share email with attachment in android pro-grammatically.
Here is the code for attached single image from gallery and camera.
OUT PUT :
1. Attached image with Email:
2.Attached Multiple images/PDF with Email:
Here is the method for attached multiple images with mail using Uri.
-Here is the method which get all image path from directory
Now this is the way to manage whole this,
3.Attached SelectMultiFiles images with Email:
here is the example for add selection file from gallery and attached with Mail.
You may also like "Send mail with background" visit
Here is the code for attached single image from gallery and camera.
OUT PUT :
1. Attached image with Email:
final int RQS_GALARRY = 0;
final int RQS_CAMERA = 2;
Uri imageUri = null;
Bitmap bmp;
ImageView YD_image;
btnGal.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Intent
intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent,
RQS_GALARRY);
}
});
btnCamera.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
String
fileName = "temp.png";
ContentValues
values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE,
fileName);
imageUri
= getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
values);
Intent
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
imageUri);
startActivityForResult(intent,
RQS_CAMERA);
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent
data)
{
super.onActivityResult(requestCode,
resultCode, data);
if (resultCode ==
RESULT_OK)
{
switch(requestCode)
{
case RQS_GALARRY:
try
{
System.gc();
imageUri
= data.getData();
String
selectedImagePath = getPath(imageUri);
if(bmp != null &&
!bmp.isRecycled())
{
bmp
= null;
((BitmapDrawable)YD_image.getDrawable()).getBitmap().recycle();
}
bmp
= BitmapFactory.decodeFile(selectedImagePath);
YD_image.setBackgroundResource(0);
YD_image.setImageBitmap(bmp);
mpopup.dismiss();
dialog_message("Prescription
uploaded");
}
catch (Exception e)
{
e.printStackTrace();
}
break;
case RQS_CAMERA:
try
{
if(bmp != null &&
!bmp.isRecycled())
{
bmp
= null;
}
bmp
= (Bitmap) data.getExtras().get("data");
YD_image.setImageBitmap(bmp);
mpopup.dismiss();
dialog_message("Prescription
uploaded");
}
catch (Exception e)
{
e.printStackTrace();
}
break;
}
}
}
public String getPath(Uri
uri)
{
String[]
projection = { MediaStore.Images.Media.DATA };
Cursor
cursor = managedQuery(uri, projection, null, null, null);
int column_index =
cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return
cursor.getString(column_index);
}
private void SendMail()
{
String
body ="Hasmukh
Bhadani":
final Intent emailIntent =
new
Intent(android.content.Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL,new String[]{Toemail});
emailIntent.setType("text/html");
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
"Subject");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,
Html.fromHtml(body));
try
{
if(imageUri != null)
{
emailIntent.putExtra(Intent.EXTRA_STREAM,
imageUri);
emailIntent.setType("image/png");
}
else
{
emailIntent.setType("plain/text");
}
startActivity(Intent.createChooser(emailIntent,"Send
Mail........"));
}
catch
(android.content.ActivityNotFoundException ex)
{
Toast.makeText(Act_YourDetails.this, "There are no
email clients installed.", Toast.LENGTH_SHORT).show();
}
}
2.Attached Multiple images/PDF with Email:
Here is the method for attached multiple images with mail using Uri.
public void SendEmail(Context context, String emailTo, String subject, String body, List<String> filePaths) { final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE); emailIntent.setType("text/html"); emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{emailTo}); //emailIntent.putExtra(android.content.Intent.EXTRA_CC,new String[]{emailCC}); emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject); emailIntent.putExtra(Intent.EXTRA_TEXT,Html.fromHtml(body)); emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); emailIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); ArrayList<Uri> uris = new ArrayList<Uri>(); for (String file : filePaths) { File fileIn = new File(file); Uri u = Uri.fromFile(fileIn); uris.add(u); } emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris); context.startActivity(Intent.createChooser(emailIntent, "Send mail...")); }
-Here is the method which get all image path from directory
public ArrayList<String> GetImagesPath(String dir) { ArrayList<String> f = new ArrayList<String>();// list of file paths File[] listFile; try { File file= new File(dir); if (file.isDirectory()) { listFile = file.listFiles(); for (int i = 0; i < listFile.length; i++) { f.add(listFile[i].getAbsolutePath()); } } } catch (Exception e) { e.printStackTrace(); } return f; }
Now this is the way to manage whole this,
public static String Directory = Environment.getExternalStorageDirectory() + "/HB/Images/"; String EmailTo="hb@gmail.com"; String Subject=Hello; String body="This is Android testing mail" ; ArrayList<String> arry_imagesPath =application.GetImagesPath(Directory);
here is the example for add selection file from gallery and attached with Mail.
import java.util.ArrayList; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.View; import android.view.View.OnClickListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; public class MainActivity extends Activity { EditText edittextEmailAddress; EditText edittextEmailSubject; EditText edittextEmailText; Button btnAddFile, btnSend; ListView listViewFiles; ArrayList<Uri> arrayUri = new ArrayList<Uri>(); ArrayAdapter<Uri> myFileListAdapter; final int RQS_LOADIMAGE = 0; final int RQS_SENDEMAIL = 1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); edittextEmailAddress = (EditText)findViewById(R.id.email_address); edittextEmailSubject = (EditText)findViewById(R.id.email_subject); edittextEmailText = (EditText)findViewById(R.id.email_text); btnAddFile = (Button)findViewById(R.id.addphoto); btnSend = (Button)findViewById(R.id.send); btnAddFile.setOnClickListener(btnAddFileOnClickListener); btnSend.setOnClickListener(btnSendOnClickListener); myFileListAdapter = new ArrayAdapter<Uri>(MainActivity.this, android.R.layout.simple_list_item_1,arrayUri); listViewFiles = (ListView)findViewById(R.id.filelist); listViewFiles.setAdapter(myFileListAdapter); } OnClickListener btnAddFileOnClickListener = new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intent, RQS_LOADIMAGE); }}; OnClickListener btnSendOnClickListener = new OnClickListener() { @Override public void onClick(View v) { String emailAddress = edittextEmailAddress.getText().toString(); String emailSubject = edittextEmailSubject.getText().toString(); String emailText = edittextEmailText.getText().toString(); String emailAddressList[] = {emailAddress}; Intent intent = new Intent(); intent.putExtra(Intent.EXTRA_EMAIL, emailAddressList); intent.putExtra(Intent.EXTRA_SUBJECT, emailSubject); intent.putExtra(Intent.EXTRA_TEXT, emailText); if(arrayUri.isEmpty()) { //Send email without photo attached intent.setAction(Intent.ACTION_SEND); intent.setType("plain/text"); } else if(arrayUri.size() == 1) { //Send email with ONE photo attached intent.setAction(Intent.ACTION_SEND); intent.putExtra(Intent.EXTRA_STREAM, arrayUri.get(0)); intent.setType("image/*"); } else { //Send email with MULTI photo attached intent.setAction(Intent.ACTION_SEND_MULTIPLE); intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, arrayUri); intent.setType("image/*"); } startActivity(Intent.createChooser(intent, "Choice App to send email:")); }}; @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { switch(requestCode) { case RQS_LOADIMAGE: Uri imageUri = data.getData(); arrayUri.add(imageUri); myFileListAdapter.notifyDataSetChanged(); break; case RQS_SENDEMAIL: break; } } } }
You may also like "Send mail with background" visit
I
will be happy if you will provide your feedback or follow this blog. Any suggestion
and help will be appreciated.
Thank
you :)
No comments:
Post a Comment