I have an android application which sends a .csv file as attachement, i have given permsissions in the manifest and reffered to lot of codes but everytime when i send an attachement, the mail comes without the attachement. I have referred to many stackoverflow solutions but they do not have effect at all.
here is the code.
sendmail.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
sendEmail();
}
protected void sendEmail() {
Log.i("Send email", "");
String TO = email.getText().toString();
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.setType("text/plain");
emailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{TO});
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("excerDB.csv"));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Your ward's academic details are here");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Please find the details attached....");
startActivity(emailIntent);
try {
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
finish();
Log.i("Finished sending email...", "");
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(DisplayContact.this,
"There is no email client installed.", Toast.LENGTH_SHORT).show();
}
}
});
P.S :- i tried it running on gennymotion emulator and on a real device.
Attachement not getting attached in android while sending mail
Becuase you are not passing right path of csv file to Uri.parse.
To create valid URI you should provide complete path of file :
Uri uriFile=Uri.parse(<File_Location_On_Int./Ext. Sd_Card>+"/excerDB.csv");
Now use uriFile as EXTRA_STREAM key value.
You EXTRA_STREAM variable is wrong :
try below :
Open a gallery to choose file from button click :
public void openGallery() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra("return-data", true);
startActivityForResult(
Intent.createChooser(intent, "Complete action using"),
PICK_FROM_GALLERY);
}
On Activity Result save the URI ;
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_FROM_GALLERY && resultCode == RESULT_OK) {
/**
* Get Path, chnage type accordingly
*/
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
cursor.moveToFirst();
columnIndex = cursor.getColumnIndex(filePathColumn[0]);
attachmentFile = cursor.getString(columnIndex);
Log.e("Attachment Path:", attachmentFile);
URI = Uri.parse("file://" + attachmentFile);
cursor.close();
}
}
Related
((HomeActivity) getActivity()).contactus
.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
sendEmail();
}
});
((HomeActivity) getActivity()).attachmentimageview
.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(intent, "Complete action using"),
MY_INTENT_CLICK);
}
});
return view;
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
if (requestCode == MY_INTENT_CLICK) {
if (null == data)
return;
String selectedImagePath;
Uri selectedImageUri = data.getData();
// MEDIA GALLERY
selectedImagePath = ImageFilePath.getPath(
getActivity(), selectedImageUri);
Log.i("Image File Path", "" + selectedImagePath);
// txta.setText("File Path : \n" + selectedImagePath);
}
}
}
private void sendEmail() {
try {
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
String[] recipients = new String[] { "Enter email" };
emailIntent
.putExtra(android.content.Intent.EXTRA_EMAIL, recipients);
emailIntent
.putExtra(
Intent.EXTRA_EMAIL,
new String[] { "anilkumar#softageindia.com,danyalozair#gmail.com" });
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
"Feedback");
emailIntent.putExtra(Intent.EXTRA_STREAM, selectedImagePath );
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,
Html.fromHtml(""));
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "HI"
+ "\n\n" + contactustext.getText().toString());
emailIntent.setType("message/rfc822");
startActivity(emailIntent);
} catch (Exception e) {
e.printStackTrace();
}
}
This is my code i want to attach file from Sd card or gallery i am using given code i am able to get path from galley But when i click on contact Us Button then it same work to get file directory if we not use attachment then it work properly with text please check where am doing wrong and how to fix it please suggest me actully i want send some text and also with attachment send via gmail when i click on button contact us it redirect to attachment and text to gmail then we can send it .
you can attach file as :
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("vnd.android.cursor.dir/email");
sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "Please find attachment");
sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+"you file's path"));
startActivity(Intent.createChooser(sharingIntent, "Attach using..."));
Firstly, create this method in your Activity or Fragment outside of onCreate
public static void getVcardString() {
String path = null;
String vfile = null;
vfile = "Contacts.vcf";
Cursor phones = mContext.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
null, null, null);
phones.moveToFirst();
Log.i("Number of contacts", "cursorCount" +phones.getCount());
for(int i =0;i<phones.getCount();i++) {
String lookupKey = phones.getString(phones.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
Log.i("lookupKey", " " +lookupKey);
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
AssetFileDescriptor fd;
try {
fd = mContext.getContentResolver().openAssetFileDescriptor(uri, "r");
FileInputStream fis = fd.createInputStream();
byte[] buf = new byte[(int) fd.getDeclaredLength()];
fis.read(buf);
String VCard = new String(buf);
path = Environment.getExternalStorageDirectory().toString() + File.separator + vfile;
FileOutputStream mFileOutputStream = new FileOutputStream(path, true);
mFileOutputStream.write(VCard.toString().getBytes());
phones.moveToNext();
filevcf = new File(path);
Log.i("file", "file" +filevcf);
}catch(Exception e1) {
e1.printStackTrace();
}
}
Log.i("TAG", "No Contacts in Your Phone");
}
and call it inside onCreate like:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getVcardString();
}
And now again, create a new method to send Email outside of onCreate like :
protected void data() {
File filelocation = filevcf ;
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("vnd.android.cursor.dir/email");
sharingIntent.setType("application/x-vcard");
sharingIntent.putExtra(Intent.EXTRA_EMAIL, "mail#gmail.com" );
sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+filelocation.getAbsolutePath()));
startActivity(Intent.createChooser(sharingIntent, "Send email"));
}
And call this data() method onClick of your send email button like :
data();
Please let me know if you get any problem now.
I am allowing user to browse native gallery > select image, and trying to attach selected image to email, but not getting exact image, see my code:
private static int RESULT_LOAD_IMAGE = 1;
String picturePath;
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
picturePath = cursor.getString(columnIndex);
cursor.close();
// getting exact image which i selected with full path
Toast.makeText(getApplicationContext(), picturePath.toString(), Toast.LENGTH_LONG).show();
sendImage();
}
}
// to attach an image to email
public void sendImage() {
// TODO Auto-generated method stub
Intent i = new Intent(Intent.ACTION_SEND);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_STREAM, Uri.parse(picturePath));
i.putExtra(Intent.EXTRA_EMAIL, new String[]{"recepient#mail.cin"});
i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
i.putExtra(Intent.EXTRA_TEXT, "body of email");
startActivity(Intent.createChooser(i,"Send email via:"));
}
}
What i am missing ? where i am doing mistake ?
Try this for attaching image.
File file = getFileStreamPath(EMAIL_TEMP_FILE);
i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
i.setType("image/jpeg");
i.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+file.getAbsolutePath())); //Your image file
Hope it helps.
how to get file path using intent. I am using this code to choose the file
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
// intent.setData(uri);
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(
Intent.createChooser(intent, "Select a File to Upload"),PICKFILE_RESULT_CODE);
} catch (android.content.ActivityNotFoundException ex)
{// Potentially direct the user to the Market with a Dialog
Toast.makeText(getApplicationContext(), "Please install a File Manager.", Toast.LENGTH_LONG).show();
}
now i want to get actual file path in
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICKFILE_RESULT_CODE && resultCode == RESULT_OK
&& data != null) {
Uri selectedFile = data.getData();
String[] filePathColum = { };
Cursor cursor = getContentResolver().query(selectedFile,
filePathColum, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColum[0]);
String FilePath = cursor.getString(columnIndex);
but when i try to get file path it crashed
how i get file path
I need to open the images gallery via code in my app.(only opening the gallery, the user is not going to select any image). I searched and found lots of ways but some of them worked only for selecting an image and other ways never worked.e.g.
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);//
startActivityForResult(Intent.createChooser(intent, "Select Picture"),SELECT_IMAGE);
or
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(
"content://media/internal/images/media"));
startActivity(intent);
how may I open the gallery?
public void pickPhoto(View view)
{
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),1);
}
Here:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Image captured and saved to fileUri specified in the Intent
Toast.makeText(this, "Image saved to:\n" +
data.getData(), Toast.LENGTH_LONG).show();
Uri curImageURI = data.getData();
Bitmap bit = getRealPathFromURI(curImageURI);
imageView.setImageBitmap(bit);
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
} else {
// Image capture failed, advise user
}
}
and
public String getRealPathFromURI(Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
#SuppressWarnings("deprecation")
android.database.Cursor cursor = managedQuery(contentUri, proj, null,
null, null);
int column_index;
try {
column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} catch (Exception e) {
return null;
}
}
CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE is the final number in the "startActivityForResult(Intent.createChooser(intent, "Select Picture"),1);"
in this case '1' so you'd need to change it to 1 or declare it as a global variable
hi i am not able to getting call to method onActivityResult after below code.
private void ImageChooseOptionDialog() {
Log.i(TAG, "Inside ImageChooseOptionDialog");
String[] photooptionarray = new String[] { "Take a photo",
"Choose Existing Photo" };
Builder alertDialog = new AlertDialog.Builder(TabSample.tabcontext)
.setItems(photooptionarray,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
if (which == 0) {
Log.i(TAG, "Which" + which);
Log.i(TAG,
"Inside ImageChooseOptionDialog Camera");
_path = Environment
.getExternalStorageDirectory()
+ File.separator
+ "TakenFromCamera.png";
Log.d(TAG, "----- path ----- " + _path);
media = _path;
// File file = new File(_path);
// Uri outputFileUri = Uri.fromFile(file);
// Intent intent = new Intent(
// android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
// intent.putExtra(MediaStore.EXTRA_OUTPUT,
// outputFileUri);
// startActivityForResult(intent, 1212);
} else if (which == 1) {
Log.i(TAG, "Which" + which);
Log.i(TAG,
"Inside ImageChooseOptionDialog Gallary");
// Intent intent = new Intent();
// intent.setType("image/*");
// intent.setAction(Intent.ACTION_GET_CONTENT);
// startActivityForResult(Intent
// .createChooser(intent,
// "Select Picture"), 1);
Intent intent = new Intent(
Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
// intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent
.createChooser(intent,
"Select Picture"), 1);
Log.i(TAG, "end" + which);
}
}
});
alertDialog.setNeutralButton("Cancel",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.create().show();
}
and this is my onActivityResult method:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i(TAG, "Inside Onactivity Result");
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
Log.i(TAG, "Inside if else Onactivity Result");
// currImageURI is the global variable I’m using to hold the
// content:// URI of the image
Uri currImageURI = data.getData();
String path = getRealPathFromURI(currImageURI);
Log.i(TAG, "Inside Onactivity Result Image path" + path);
}
}
}
please let me knew where i am doing wrong. i am calling onActivityResult method from which=1 after dialog box appears.
but not getting any log inside onActivityResult method in logcat.
This is because, you may not be getting RESULT_OK. Always first check for request code and inside that check for result code. If you are retrieving image from gallery, try following code inside onActivityResult():
if (requestCode == TAKE_PICTURE_GALLERY) {
if (resultCode == RESULT_OK) {
final Uri selectedImage = data.getData();
final String[] filePathColumn = { MediaStore.Images.Media.DATA };
final Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
final int columnIndex = cursor
.getColumnIndex(filePathColumn[0]);
final String filePath = cursor.getString(columnIndex);
cursor.close();
}
}
And use the filePath wherever you want.
I hope this solves your problem. Thank you :)
UPDATE:
Use this code to start your gallery activity:
imagePathURI = Uri.fromFile(new File(<your image path>));
final Intent intent = new Intent(Intent.ACTION_PICK, imagePathURI);
intent.setType("image/*");
intent.putExtra(MediaStore.EXTRA_OUTPUT, imagePathURI);
startActivityForResult(intent, TAKE_PICTURE_GALLERY);
When you want to retrieve image from gallery, the MediaStore.EXTRA_OUTPUT refers to The name of the Intent-extra used to indicate a content resolver Uri to be used to store the requested image or video. So here, you have to pass the image path where u'll receive your image.
imagePathURI = Uri.fromFile(new File(<your image path>)); // here a file will be created from your image path. And when you'll receive an image, u can access the image from this image path.