Take picture with camera and attach uncompressed picture to email - android

Hello stackoverflow community,
I searched alot about attaching a photo to an email. I found code which worked, but the picture had a bad resolution. Now I want to send the picture uncompressed. Therefore I looked through many questions and found this piece of code,
but when taking the photo, I can't accept it and move on.
Here you can look into my code:
this.eco.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File path = new File("/Pictures/");
path.mkdirs();
String fileName = "verunreinigung.jpg";
File file = new File(path, fileName);
cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
Uri.fromFile(file));
startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE);
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
// variables
int latitude = GMapsSubActivity.getLatitude();
int longitude = GMapsSubActivity.getLongitude();
double dLat = latitude / 1000000.0;
double dLng = longitude / 1000000.0;
Log.v("lat", Integer.toString(latitude));
Log.v("lng", Integer.toString(longitude));
Log.v("lat", Double.toString(dLat));
Log.v("lng", Double.toString(dLng));
String[] address = new String[]{cursor.getString(cursor.getColumnIndex("email"))};
String subject = "Verunreinigung am Gewässer: " + cursor.getString(cursor.getColumnIndex("gewName"))
+ " / " + cursor.getString(cursor.getColumnIndex("reviergrenzen"));
String text = "Hallo " + cursor.getString(cursor.getColumnIndex("name"))
+ "!\n\nIch habe an/in Ihrem Gewässer eine"
+ " Verunreinigung entdeckt.\n"
+ "Sie befindet sich hier:\n"
+ "http://maps.google.com/?q=" + dLat + "," + dLng + "\n"
+ "Das Foto finden Sie im Anhang.\n\nLiebe Grüße!";
ContentValues values = new ContentValues();
values.put(Images.Media.TITLE, "photo");
values.put(Images.Media.BUCKET_ID, "photo_ID");
values.put(Images.Media.DESCRIPTION, "");
values.put(Images.Media.MIME_TYPE, "image/jpeg");
Uri photoUri = getContentResolver().insert(
Media.EXTERNAL_CONTENT_URI, values);
OutputStream outstream;
try {
outstream = getContentResolver().openOutputStream(photoUri);
photo.compress(Bitmap.CompressFormat.JPEG, 100, outstream);
outstream.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// define the intent
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, address);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, text);
emailIntent.putExtra(Intent.EXTRA_STREAM, photoUri);
emailIntent.setType("plain/text");
// start the intent
try {
startActivity(Intent.createChooser(emailIntent,
"Versende Email via:"));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(getApplicationContext(),
"Kein Email-Programm installiert.", Toast.LENGTH_SHORT)
.show();
}
}
}

Use the below code to send a mail
String filelocation="/mnt/sdcard/capture.png";
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("vnd.android.cursor.dir/email");
String to[] = "user#gmail.com";
sharingIntent.putExtra(Intent.EXTRA_EMAIL, to);
sharingIntent.putExtra(Intent.EXTRA_STREAM,filelocation);
sharingIntent.putExtra(Intent.EXTRA_SUBJECT,"subject");
startActivity(Intent.createChooser(sharingIntent, "Send email"));

Related

how to attach file in android using gmail

((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.

attach image from camera to email - android

I have a button which takes a photo, stores the thumbnail in an Imageview, and when the user clicks another button, it needs to attach that image to an email.
Ive followed the "Taking Photos Simply" directions from the Android site, but there is no attachment included. Can somebody see where i am going wrong, and what I need to do to fix it?
My on Activity Result code:
public void onActivityResult(int requestcode, int resultcode, Intent data) {
if (requestcode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
if (resultcode == RESULT_OK) {
Bundle bundle = new Bundle();
bundle = data.getExtras();
Bitmap BMB;
BMB = (Bitmap) bundle.get("data");
Hoon_Image.setImageBitmap(BMB);
try {
createImageFile();
} catch (IOException e) {
e.printStackTrace();
}
mDrawable = Hoon_Image.getDrawable();
mBitmap = ((BitmapDrawable) mDrawable).getBitmap();
}
}
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
}
my email intent:
if (Witness_response == "Yes"){
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"roadsafety.app#shellharbour.nsw.gov.au"});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Dob in a Hoon Report (Y)");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Hoon report has been recieved " + emailBody);
emailIntent.putExtra(Intent.EXTRA_STREAM, mCurrentPhotoPath );
emailIntent.setType("message/rfc822");
startActivity(Intent.createChooser(emailIntent, "Choose email client..."));
} else if (Witness_response == "No"){
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"roadsafety.app#shellharbour.nsw.gov.au"});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Dob in a Hoon Report (N)");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Hoon report has been recieved " + emailBody);
emailIntent.putExtra(Intent.EXTRA_STREAM, mCurrentPhotoPath);
emailIntent.setType("message/rfc822");
startActivity(Intent.createChooser(emailIntent, "Choose email client..."));
}
try this code
public class MainActivity extends Activity {
private static final int CAMERA_REQUEST = 1888;
private ImageView imageView;
private File f;
public File getAlbumDir()
{
File storageDir = new File(
Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES
),
"BAC/"
);
// Create directories if needed
if (!storageDir.exists()) {
storageDir.mkdirs();
}
return storageDir;
}
private File createImageFile() throws IOException {
// Create an image file name
String imageFileName =getAlbumDir().toString() +"/image.jpg";
File image = new File(imageFileName);
return image;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.imageView = (ImageView)this.findViewById(R.id.imageView1);
Button photoButton = (Button) this.findViewById(R.id.button1);
photoButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
f = createImageFile();
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(cameraIntent, CAMERA_REQUEST);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap photo = BitmapFactory.decodeFile(f.getAbsolutePath());
imageView.setImageBitmap(photo);
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL , new String[]{"email id"});
i.putExtra(Intent.EXTRA_SUBJECT, "subject");
i.putExtra(Intent.EXTRA_TEXT , "body of email");
Uri uri = Uri.fromFile(f);
i.putExtra(Intent.EXTRA_STREAM, uri);
try {
startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MainActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
}
}
}
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("image/*");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"me#gmail.com"});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Test Subject");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,"go on read the emails");
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(emailIntent, "Send Mail"));;
and here,
uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), FILENAME));

Camera image returns thumbnail in Android

In my Android application, I want to capture an image from the camera and pass that image Uri to next activity.
Here, I am getting a thumbnail image of captured image. To solve that issue, I used MediaStore.EXTRA_OUTPUT for my picture capture intent.
Now, I don't know how to get the uri of currently captured image. I tried the code below and passing imageUri in next activity, but it throws NoSuchFileDirectory exception.
I referred this link, but are not getting any clear idea about getting the captured image Uri and passing it to next activity. Please help me to solve this issue. If you find any problem with this code then suggest where I am going wrong.
Code:
btnCamera.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
Intent pictureActionIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File photo = null;
try {
photo = SaveCameraImage();
} catch (Exception e) {
Log.e("Camera capture-------------",
"Can't create file to take picture!");
Toast.makeText(SelectFrameActivity.this,
"Please check SD card! Image shot is impossible!",
10000).show();
}
mImageUri = Uri.fromFile(photo);
pictureActionIntent
.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
startActivityForResult(pictureActionIntent, TAKE_PICTURE);
}
});
private File SaveCameraImage() {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
String dt = sdf.format(new Date());
imageFile = null;
imageFile = new File(Environment.getExternalStorageDirectory(),
"FrameFace/" + "Camera_" + dt + ".png");
Log.e("New Camera Image Path:-",
Environment.getExternalStorageDirectory() + "/FrameFace/"
+ "Camera_" + dt + ".png");
if (!imageFile.exists())
imageFile.createNewFile();
bmp_camera_image.compress(Bitmap.CompressFormat.PNG, 100,
new FileOutputStream(imageFile));
Log.e("imagetext", "the file has saved to the sdcard");
imagePath = Environment.getExternalStorageDirectory()
+ "/FrameFace/" + "Camera_" + dt + ".png";
Log.e("FaceFrameActivity - SaveCameraImage: ", "Image is Saved to"
+ imagePath);
} catch (Exception ex) {
ex.printStackTrace();
}
return imageFile;
}
#Override
protected void onActivityResult(int requestCode, int resultCode,
final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case TAKE_PICTURE:
Log.e("result code: ", Integer.toString(Activity.RESULT_OK) + " "
+ Integer.toString(resultCode));
if (resultCode == Activity.RESULT_OK) {
// bmp_camera_image = (Bitmap) data.getExtras().get("data");
SaveCameraImage();
// this.getContentResolver().notifyChange(mImageUri, null);
// ContentResolver cr = this.getContentResolver();
// try {
// GlobalClass.cameraBitmap =
// android.provider.MediaStore.Images.Media
// .getBitmap(cr, mImageUri);
// } catch (FileNotFoundException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
Handler h = new Handler();
h.postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(getApplicationContext(),
FaceCropActivity_dual.class);
// intent.putExtra("imagePath", imagePath);
intent.putExtra("imagePath", mImageUri.toString());
startActivity(intent);
}
}, 500);
}
break;
}
}
I used this code and its working for me to save camera image and getting uri of that image.
btnCamera.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try {
dialog.dismiss();
SimpleDateFormat sdf = new SimpleDateFormat(
"yyyyMMdd_HHmmss");
String dt = sdf.format(new Date());
imageFile = null;
imageFile = new File(Environment
.getExternalStorageDirectory() + "/FrameFace/",
"Camera_" + dt + ".png");
Log.e("New Camera Image Path:-",
Environment.getExternalStorageDirectory()
+ "/FrameFace/" + "Camera_" + dt + ".png");
if (!imageFile.exists())
imageFile.createNewFile();
imagePath = Environment.getExternalStorageDirectory()
+ "/FrameFace/" + "Camera_" + dt + ".png";
Uri outputFileUri = Uri.fromFile(imageFile);
Intent pictureActionIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
pictureActionIntent.putExtra(MediaStore.EXTRA_OUTPUT,
outputFileUri);
startActivityForResult(pictureActionIntent, TAKE_PICTURE);
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
#Override
protected void onActivityResult(int requestCode, int resultCode,
final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case TAKE_PICTURE:
Log.e("result code: ", Integer.toString(Activity.RESULT_OK) + " "
+ Integer.toString(resultCode));
if (resultCode == Activity.RESULT_OK) {
Handler h = new Handler();
h.postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(getApplicationContext(),
FaceCropActivity_dual.class);
// intent.putExtra("imagePath", imagePath);
Log.e("Image Path-------------", "" + imagePath);
intent.putExtra("imagePath", imagePath);
startActivity(intent);
}
}, 500);
}
break;
}
}
imageFile = new File(Environment.getExternalStorageDirectory(),
"FrameFace/" + "Camera_" + dt + ".png");
here your creating subdirectory and a file in one shot. you create directory then image file
update
try this
imageFile = new File(Environment.getExternalStorageDirectory()+"/FrameFace" ,"Camera_" + dt + ".png");
remove this from oncick listener
File photo = null;
try {
photo = SaveCameraImage();
} catch (Exception e) {
Log.e("Camera capture-------------",
"Can't create file to take picture!");
Toast.makeText(SelectFrameActivity.this,
"Please check SD card! Image shot is impossible!",
10000).show();
}
I created a wrapper to help you with that. See the whole discussion on stackoverflow here or find the code on github.

Trying to display the picture in my app that was just taken with the built in camera app

I'm trying to make a app that will allow the user to take a photo then display it in the app and save it.
I'm using the following code to take the picture (from Google)
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePictureIntent, 1);
How can I tell when the user presses the back button and return to my app so I can display the image???
You did startActivityForResult in the same activity
you need to call
onActivityResult(int requestcode, int resultCode, Intent data){
if(resultCode==RESULT_OK){
if(requestCode==1){
// get your data from the intent
}
}
}
Take Picture with this code...
File pictureFileDir = getDir();
if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) {
Log.d("Photo Take", "Can't create directory to save image.");
Toast.makeText(PhotoTake.this , "Can't create directory to save image.",
Toast.LENGTH_LONG).show();
return;
}
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
String date = dateFormat.format(new Date());
String photoFile = "Picture_" + date + ".jpg";
String filepath = pictureFileDir.getPath() + File.separator;
File imageFile = new File(filepath , photoFile);
ContentValues image = new ContentValues();
image.put(Images.Media.TITLE, photoFile);
image.put(Images.Media.DISPLAY_NAME, photoFile);
image.put(Images.Media.DESCRIPTION, "Accident data Accachment " + date);
image.put(Images.Media.DATE_ADDED, date);
image.put(Images.Media.DATE_TAKEN, date);
image.put(Images.Media.DATE_MODIFIED, date);
image.put(Images.Media.MIME_TYPE, "image/jpeg");
image.put(Images.Media.ORIENTATION, 0);
File parent = imageFile.getParentFile();
String path = parent.toString().toLowerCase();
String name = parent.getName().toLowerCase();
image.put(Images.ImageColumns.BUCKET_ID, path.hashCode());
image.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, name);
image.put(Images.Media.SIZE, imageFile.length());
image.put(Images.Media.DATA, imageFile.getAbsolutePath());
mCapturedImageURI = PhotoTake.this.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, image);
Intent intent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
mCapturedImageURI);
startActivityForResult(intent, req);
Now retrieve it in onActivityResult...
if (requestCode == 100 && resultCode == Activity.RESULT_OK) {
if (mCapturedImageURI != null) {
img1.setImageBitmap(getScaledBitmap(mCapturedImageURI));;
System.out.println("Onactivity Result uri = " + mCapturedImageURI.toString());
} else {
Toast.makeText(PhotoTake.this, "Error getting Image",
Toast.LENGTH_SHORT).show();
}
}
You'll require this method to avoid OutOfMemory Exception
private Bitmap getScaledBitmap(Uri uri){
Bitmap thumb = null ;
try {
ContentResolver cr = getContentResolver();
InputStream in = cr.openInputStream(uri);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize=8;
thumb = BitmapFactory.decodeStream(in,null,options);
} catch (FileNotFoundException e) {
Toast.makeText(PhotoTake.this , "File not found" , Toast.LENGTH_SHORT).show();
}
return thumb ;
}
Hope it helps.

Android: send an email with an image from an ImageView

I'm new here on stackoverflow. I have a little problem with my Android app, expecially with an ImageView that triggers an event on tap. This event opens an email client with some pre-written text and it should attach the image of the Image. I already know that the image should be converted into a bitmap before, then compressed and send it to the email client, but unfortunatly I'm not an Android/Java expert so I can't find how to do that. This is the code of the email method:
new code below
Where I have to replace "String imageURI = null;" with what the email needs as image. Thank you all!
EDIT:
I managed to edit my code to this, that gives no errors:
public void sendMail(ImageView image){
Intent i = new Intent(Intent.ACTION_SEND);
int imageURI = R.drawable.img1;
i.setType("text/plain");
i.putExtra(Intent.EXTRA_EMAIL , new String[]{"destinatario#globelife.biz"});
i.putExtra(Intent.EXTRA_SUBJECT, "Oggetto");
i.putExtra(Intent.EXTRA_TEXT , "Globelife");
i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
i.setType("image/jpeg");
i.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://"+getPackageName()+"/"+imageURI));
try {
startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(Test01Activity.this, "Non sono presenti app per l'invio di e-mails.", Toast.LENGTH_SHORT).show();
}
}
But I need to change "int imageURI = R.drawable.img1;" to "int imageURI = ImageView.src;" or something like that
try this
ImageView iv = (ImageView) findViewById(R.id.splashImageView);
Drawable d =iv.getBackground();
BitmapDrawable bitDw = ((BitmapDrawable) d);
Bitmap bitmap = bitDw.getBitmap();
File mFile = savebitmap(bitmap);
and then
Uri u = null;
u = Uri.fromFile(mFile);
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("image/*");
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Hello...");
// + "\n\r" + "\n\r" +
// feed.get(Selectedposition).DETAIL_OBJECT.IMG_URL
emailIntent.putExtra(Intent.EXTRA_TEXT, "Your tsxt here");
emailIntent.putExtra(Intent.EXTRA_STREAM, u);
startActivity(Intent.createChooser(emailIntent, "Send email..."));
and savebitmap method
private File savebitmap(Bitmap bmp) {
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
OutputStream outStream = 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;
}
Intent i = new Intent(Intent.ACTION_SEND);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setType("image/jpg");
i.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/Pictures/
image.jpg"));
startActivity(i);
Intent intent=new Intent(Intent.ACTION_SEND);
String[] recipients={"destinatario#domain.com"};
intent.putExtra(Intent.EXTRA_EMAIL, recipients);
intent.putExtra(Intent.EXTRA_SUBJECT, "Oggetto");
intent.putExtra(Intent.EXTRA_TEXT , "Testo");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setType("image/png");
intent.putExtra(Intent.EXTRA_STREAM,Uri.parse(“file///sdcard/Images/your_image.jpg”));//or you can pass the path of your image
startActivity(Intent.createChooser(intent, "Send mail"));
//re move the String imageURI=null;
public void sendMail(ImageView image){
Intent i = new Intent(Intent.ACTION_SEND);
Uri pngImageUri = Uri.parse(image);
i.setType("image/png");//change here with image/png
i.putExtra(Intent.EXTRA_EMAIL , new String[]{"destinatario#domain.com"});
i.putExtra(Intent.EXTRA_SUBJECT, "Oggetto");
i.putExtra(Intent.EXTRA_TEXT , "Testo");
i.putExtra(Intent.EXTRA_STREAM, pngImageUri);

Categories

Resources