Sending ImageView as attachment in Email-android - android

I am learning android and working on an application to takes pictures and send them through email. I have got the picture in ImageView in the code below, but am not sure how to send this picture as an email attachment, without saving the picture to file on the device.
Ideally i would like to know if that is possible? If yes can you point me the correct direction on how to implement the same. Also(optionally) if the picture can be compressed.
public class EmailPic extends Activity implements OnClickListener{
ImageButton ibEmail;
Button bEmail;
ImageView ivEmail;
Intent intentEmail;
Bitmap bmpEmail;
final static int picData = 0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pic_email);
initializeVars();
InputStream is = getResources().openRawResource(R.drawable.ic_launcher);
bmpEmail = BitmapFactory.decodeStream(is);
}
private void initializeVars() {
ibEmail = (ImageButton)findViewById(R.id.ibTakePicEmail) ;
ivEmail = (ImageView)findViewById(R.id.ivPicEmail);
bEmail = (Button) findViewById(R.id.bSendPicEmail);
bEmail.setOnClickListener(this);
ibEmail.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.ibTakePicEmail:
intentEmail = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intentEmail,picData);
break;
case R.id.bSendPicEmail:
String message = "email Body";
String[] recipients = new String[]{"mymail.com"};
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("application/image");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,recipients);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "My Subject");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,message);
startActivity(emailIntent);
break;
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
Bundle ext = data.getExtras();
bmpEmail = (Bitmap)ext.get("data");
//Log.d("StatusActivity","bmpEmail >>"+bmpEmail);
ivEmail.setImageBitmap(bmpEmail);
}
}
}

Try out as below:
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("image/*");
Uri uri = Uri.parse("android.resource://your package name/"+R.drawable.ic_launcher);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.putExtra(android.content.Intent.EXTRA_EMAIL,recipients);
shareIntent.putExtra(Intent.EXTRA_TEXT, message);
startActivity(Intent.createChooser(shareIntent, "Send your image"));
EDITED:
Declare the File variable
File pic;
In your OnActivityResult() apply changes as below:
Bundle ext = data.getExtras();
bmpEmail = (Bitmap)ext.get("data");
try {
File root = Environment.getExternalStorageDirectory();
if (root.canWrite()){
pic = new File(root, "pic.png");
FileOutputStream out = new FileOutputStream(pic);
bmpEmail.compress(CompressFormat.PNG, 100, out);
out.flush();
out.close();
}
} catch (IOException e) {
Log.e("BROKEN", "Could not write file " + e.getMessage());
}
And in your send email code add the line
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(pic));

So, You want to capture the image from the ImageView, compress it and then attach.
Basically the process is :
Get the Image from the ImageView.
Convert to Bitmap.
Save it.(You have to do it in any case, if you want to attach it)
Hopefully you can delete it afterwards.
Attach to email Intent.
First of all get the cached bitmap of the ImageView
Bitmap myBitmap = yourImageView.getDrawingCache();
This will return the cached bitmap from the ImageView.
Compress it and save
URI FILENAME; //URI For the ImageView, we need earlier to send
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_WORLD_READABLE);
myBitmap.compress(Bitmap.CompressFormat.PNG, 0, fos);
fos.close();
Create your Send Intent
String message = "email Body";
String[] recipients = new String[]{"mymail.com"};
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("application/image");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,recipients);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "My Subject");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,message);
emailIntent.putExtra(Intent.EXTRA_STREAM, FILENAME); //this line is added to your code
startActivity(emailIntent);
You can delete the image afterward if you need to.

Related

How do I share Images from Firebase Database?

I have created database and there is Url of image and I want to show that image in my app which is working but now I want share that image in Whatsapp etc. Now what I need to change there as I didn't get any idea,how to do it?
Here is my Code:
#Override
public void onBindViewHolder(#NonNull final myViewHolder holder, int position) {
Picasso.get().load(mdata.get(holder.getAdapterPosition()).getImage()).into(holder.imgView);
holder.shareBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent;
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), holder.imgView.getId());
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) +"/share.png";
FileOutputStream out = null;
File file = new File(path);
try{
out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG,100,out);
out.flush();
out.close();
}catch(Exception e){
e.printStackTrace();
}
path = file.getPath();
Uri bmpUri = Uri.parse("file://" + path);
intent = new Intent(Intent.ACTION_SEND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_STREAM, bmpUri);
intent.setType("image/jpeg");
intent.putExtra(Intent.EXTRA_TEXT," Hello Your's Images ");
context.startActivity(Intent.createChooser(intent, " Share Images Via"));
You can do it using uri, and try to implement the next code:
Uri imageUri = Uri.parse(pictureFile.getAbsolutePath());
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
//Send to Whattssap
shareIntent.setPackage("com.whatsapp");
//Even you can add text to the image
shareIntent.putExtra(Intent.EXTRA_TEXT, picture_text);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/jpeg");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
try {
startActivity(shareIntent);
} catch (android.content.ActivityNotFoundException ex) {
ToastHelper.MakeShortText("Whatsapp have not been installed.");
}
And that's all

How to attach images from different paths to an email?

I am using an intent to send email with subject, content and attached images
everything works fine except some of the images don't get attached
File path for successfully attached photos is like:
content://media/external/images/media/14960
File path for Unsuccessfully attached photos is like:
content://com.android.providers.media.documents/document/image%3A14745
I am using this code
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("message/rfc822");
//emailIntent.setType("application/image");
emailIntent.putExtra(Intent.EXTRA_EMAIL , new String[]{""});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, mNotes.get(position).getTitle());
emailIntent.putExtra(Intent.EXTRA_TEXT , mNotes.get(position).getContent());
ArrayList<String[]> photos = StringManipulation.imgDeserialize(mNotes.get(position).getImgUrls());
for (String[] photo : photos){
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(photo[0]));
}
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
How can i made all the photos get attached to the email?
I got this Uris by taking permissions for CAMERA, READ and WRITE EXTERNAL STORAGE
//.... onCreate...
//initialize the textview for starting the camera
TextView takePhoto = (TextView) view.findViewById(R.id.tvTakeCameraPhoto);
takePhoto.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "onClick: starting camera.");
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, Permissions.CAMERA_REQUEST_CODE);
}
});
//Initialize the textview for choosing an image from memory
TextView selectPhoto = (TextView) view.findViewById(R.id.tvChoosePhotoFromMemory);
selectPhoto.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "onClick: accessing phones memory.");
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, Permissions.PICK_FILE_REQUEST_CODE);
}
});
//.................
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
/*
Results when taking a new image with camera
*/
if(requestCode == Permissions.CAMERA_REQUEST_CODE && resultCode == Activity.RESULT_OK){
Log.d(TAG, "onActivityResult: done taking a picture.");
//get the new image bitmap
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
Log.d(TAG, "onActivityResult: received bitmap: " + bitmap);
// CALL THIS METHOD TO GET THE URI FROM THE BITMAP
Uri tempUri = getImageUri(getActivity(), bitmap);
// CALL THIS METHOD TO GET THE ACTUAL PATH
File takenPhotoFile = new File(tempUri.toString());
mOnPhotoReceived.getImagePath(takenPhotoFile.getPath());
getDialog().dismiss();
}
/*
Results when selecting new image from phone memory
*/
if(requestCode == Permissions.PICK_FILE_REQUEST_CODE && resultCode == Activity.RESULT_OK){
Log.d(TAG, "onActivityResult: done choosing image");
Uri selectedImageUri = data.getData();
File file = new File(selectedImageUri.toString());
Log.d(TAG, "onActivityResult: images: " + file.getPath());
//send the bitmap and fragment to the interface
mOnPhotoReceived.getImagePath(file.getPath());
getDialog().dismiss();
}
}
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
then passing the listener to the activity.
First, your app on its own needs permissions to read the content identified by those Uri values. You do not indicate where and how you get those Uri values, but you only have a short time to use them, in general. If you do not have read access to the content, you will be out of luck.
Second, add this line before the startActivity() call:
emailIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
This passes your read permission along to whatever app processes this Intent.

How to Share Image + Text together using ACTION_SEND in android?

I want to share Text + Image together using ACTION_SEND in android, I am using below code, I can share only Image but i can not share Text with it,
private Uri imageUri;
private Intent intent;
imageUri = Uri.parse("android.resource://" + getPackageName()+ "/drawable/" + "ic_launcher");
intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, "Hello");
intent.putExtra(Intent.EXTRA_STREAM, imageUri);
intent.setType("image/*");
startActivity(intent);
Any help on this ?
You can share plain text with the following code
String shareBody = "Here is the share content body";
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, getResources().getString(R.string.share_using)));
So your full code (your image + text) becomes
private Uri imageUri;
private Intent intent;
imageUri = Uri.parse("android.resource://" + getPackageName() +
"/drawable/" + "ic_launcher");
intent = new Intent(Intent.ACTION_SEND);
// text
intent.putExtra(Intent.EXTRA_TEXT, "Hello");
//image
intent.putExtra(Intent.EXTRA_STREAM, imageUri);
//type of content
intent.setType("*/*");
//sending
startActivity(intent);
I just replaced image/* with */*
Update:
Uri imageUri = Uri.parse("android.resource://" + getPackageName() +
"/drawable/" + "ic_launcher");
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, "Hello");
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/jpeg");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "send"));
please have a look on this code worked for me to share an text and image together
Intent shareIntent;
Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher);
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+"/Share.png";
OutputStream out = null;
File file=new File(path);
try {
out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
path=file.getPath();
Uri bmpUri = Uri.parse("file://"+path);
shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
shareIntent.putExtra(Intent.EXTRA_TEXT,"Hey please check this application " + "https://play.google.com/store/apps/details?id=" +getPackageName());
shareIntent.setType("image/png");
startActivity(Intent.createChooser(shareIntent,"Share with"));
Don't forget to give WRITE_EXTERNAL_STORAGE Permission
also in facebook it can only share the image because facebook is not allowing to share the text via intent
It is possibly because the sharing app (FB, twitter, etc) may not have permissions to read the image.
Google's document says:
The receiving application needs permission to access the data the Uri points to. The recommended ways to do this are:
http://developer.android.com/training/sharing/send.html
I am not sure the sharing apps have permissions to read an image in the bundle of our apps. But my files saved in
Activity.getFilesDir()
cannot be read. As suggested in the above link, we may consider to store images in the MediaStore, where the sharing apps have permissions to read.
Update1:
The following is working code to share image with text in Android 4.4.
Bitmap bm = BitmapFactory.decodeFile(file.getPath());
intent.putExtra(Intent.EXTRA_TEXT, Constant.SHARE_MESSAGE
+ Constant.SHARE_URL);
String url= MediaStore.Images.Media.insertImage(this.getContentResolver(), bm, "title", "description");
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse(url));
intent.setType("image/*");
startActivity(Intent.createChooser(intent, "Share Image"));
The side effect is we add an image in the MediaStore.
I have been looking for solution of this question for a while and found this one up and running, hope it helps.
private BitmapDrawable bitmapDrawable;
private Bitmap bitmap1;
//write this code in your share button or function
bitmapDrawable = (BitmapDrawable) mImageView.getDrawable();// get the from imageview or use your drawable from drawable folder
bitmap1 = bitmapDrawable.getBitmap();
String imgBitmapPath= MediaStore.Images.Media.insertImage(getContentResolver(),bitmap1,"title",null);
Uri imgBitmapUri=Uri.parse(imgBitmapPath);
String shareText="Share image and text";
Intent shareIntent=new Intent(Intent.ACTION_SEND);
shareIntent.setType("*/*");
shareIntent.putExtra(Intent.EXTRA_STREAM,imgBitmapUri);
shareIntent.putExtra(Intent.EXTRA_TEXT, shareText);
startActivity(Intent.createChooser(shareIntent,"Share Wallpaper using"));
String bitmapPath = MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "title", null);
Uri bitmapUri = Uri.parse(bitmapPath);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/png");
intent.putExtra(Intent.EXTRA_STREAM, bitmapUri);
intent.putExtra(Intent.EXTRA_TEXT, "This is a playstore link to download.. " + "https://play.google.com/store/apps/details?id=" + getPackageName());
startActivity(Intent.createChooser(intent, "Share"));
try using this code.I am uploading ic_launcher from drawable.you can change this with your file from gallary or bitmap.
void share() {
Bitmap icon = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + "temporary_file.jpg");
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
share.putExtra(Intent.EXTRA_TEXT, "hello #test");
share.putExtra(Intent.EXTRA_STREAM,
Uri.parse("file:///sdcard/temporary_file.jpg"));
startActivity(Intent.createChooser(share, "Share Image"));
}
String text = "Look at my awesome picture";
Uri pictureUri = Uri.parse("file://my_picture");
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, text);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/*");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "Share images..."));
Check the below code it worked for me
Picasso.with(getApplicationContext()).load("image url path").into(new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Intent i = new Intent(Intent.ACTION_SEND);
i.putExtra(Intent.EXTRA_TEXT, "Let me recommend you this application" +
"\n"+ "your share url or text ");
i.setType("image/*");
i.putExtra(Intent.EXTRA_STREAM, getLocalBitmapUri(bitmap));
context.startActivity(Intent.createChooser(i, "Share using"));
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
private Uri getLocalBitmapUri(Bitmap bmp) {
Uri bmpUri = null;
try {
File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), "share_image_" + System.currentTimeMillis() + ".png");
FileOutputStream out = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 50, out);
out.close();
bmpUri = Uri.fromFile(file);
} catch (IOException e) {
e.printStackTrace();
}
return bmpUri;
}
private void shareImage(){
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.drawable.Starlay_straightface_image);
File f = new File(getExternalCacheDir()+"/"+getResources().getString(R.string.app_name)+".png");
Intent shareIntent;
try {
FileOutputStream outputStream = new FileOutputStream(f);
bitmap.compress(Bitmap.CompressFormat.PNG,100,outputStream);
outputStream.flush();
outputStream.close();
shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f));
shareIntent.putExtra(Intent.EXTRA_TEXT,"Hey please check this application " + "https://play.google.com/store/apps/details?id=" +getPackageName());
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}catch (Exception e){
throw new RuntimeException(e);
}
startActivity(Intent.createChooser(shareIntent,"Share Picture"));
}
To share a drawable image, the image has to be first saved in device's cache or external storage.
We check if "sharable_image.jpg" already exists in cache, if exists, the path is retrieved from existing file.
Else, the drawable image is saved in cache.
The saved image is then shared using intent.
private void share() {
int sharableImage = R.drawable.person2;
Bitmap bitmap= BitmapFactory.decodeResource(getResources(), sharableImage);
String path = getExternalCacheDir()+"/sharable_image.jpg";
java.io.OutputStream out;
java.io.File file = new java.io.File(path);
if(!file.exists()) {
try {
out = new java.io.FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
path = file.getPath();
Uri bmpUri = Uri.parse("file://" + path);
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shareIntent.putExtra(Intent.EXTRA_TEXT, "This is a sample body with more detailed description");
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent,"Share with"));
}
I tried this solution with Android X and works perfectly with whatsapp, telegram and gmail.
This is my code:
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, "<<sharingText>>");
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("<<imageResource>>"));
shareIntent.setType("image/*");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
if (getView() != null && getView().getContext() != null &&
shareIntent.resolveActivity(getView().getContext().getPackageManager()) != null) {
getView().getContext().startActivity(Intent.createChooser(shareIntent, null));
}
By accident(the text message part, I had given up on that), I noticed that when I chose Messages App to handle the request, the Message App would open with the text from Intent.EXTRA_SUBJECT plus the image ready to send, I hope it helps.
String[] recipient = {"your_email_here"};
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_EMAIL, recipient);
intent.putExtra(Intent.EXTRA_SUBJECT, "Hello, this is the subject line");
intent.putExtra(Intent.EXTRA_TEXT, messageEditText.getText().toString());
intent.putExtra(Intent.EXTRA_STREAM, imageUri);
intent.setType("image/*");
You can this code it is working
Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.refer_image);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*" + "text/*");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(this.getContentResolver(),
b, "Title", null);
Uri imageUri = Uri.parse(path);
share.putExtra(Intent.EXTRA_STREAM, imageUri);
share.putExtra(Intent.EXTRA_TEXT, "Hey I am Subhajit");
startActivity(Intent.createChooser(share, "Share via"));
File Provider:
it is good to use FileProvider to share image to other app securely.
it work on all version of android.
i tried it and works fine.
Link:
https://developer.android.com/reference/androidx/core/content/FileProvider

How to access an image that is stored internally

I have an application that takes a picture and stores the picture internally in a folder that I have created. After taking a picture I want to be able to access it so that I can email it. How can I access the image I have just taken?Below is my code that saves the image internally after the picture has been taken:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
bmp = (Bitmap) extras.get("data");
iv.setImageBitmap(bmp);
File storagePath = new File(
Environment.getExternalStorageDirectory() + "/DavePics/");
storagePath.mkdirs();
File myImage = new File(storagePath, Long.toString(System
.currentTimeMillis()) + ".jpg");
Bitmap b = Bitmap.createScaledBitmap(bmp, 320, 480, false);
try {
FileOutputStream out = new FileOutputStream(myImage);
b.compress(Bitmap.CompressFormat.JPEG, 80, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
When I check the folder I have created the picture is there. What I want to do now is access that picture so that I can send it in an email from my code below:
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bSendPic:
String emailaddress[] = { "info#sklep.com", "", };
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL, emailaddress);
//emailIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, pic);
emailIntent.setType("image/jpeg");
startActivity(Intent.createChooser(emailIntent, "Send Mail"));
break;
case R.id.ibTakePic:
i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, cameraData);
break;
}
}
How do I access this pic so that I can add it to my email intent? Am i going about this the right way? Thanks
Edit: This is my full code
public class Camera extends Activity implements View.OnClickListener {
ImageButton ib;
Button b;
ImageView iv;
Intent i;
final static int cameraData = 0;
Bitmap bmp;
File myImage;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.photo);
initialize();
InputStream is = getResources().openRawResource(R.drawable.ic_launcher);
bmp = BitmapFactory.decodeStream(is);
}
private void initialize() {
ib = (ImageButton) findViewById(R.id.ibTakePic);
b = (Button) findViewById(R.id.bSendPic);
iv = (ImageView) findViewById(R.id.ivReturnedPic);
b.setOnClickListener(this);
ib.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bSendPic:
if (myImage.exists()) {
String emailaddress[] = { "info#sklep.com", "", };
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL, emailaddress);
emailIntent.setType("image/jpeg");
emailIntent
.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(myImage));
startActivity(Intent.createChooser(emailIntent, "Send Mail"));
}
break;
case R.id.ibTakePic:
i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, cameraData);
break;
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
bmp = (Bitmap) extras.get("data");
iv.setImageBitmap(bmp);
File storagePath = new File(
Environment.getExternalStorageDirectory() + "/DavePics/");
storagePath.mkdirs();
myImage = new File(storagePath, Long.toString(System
.currentTimeMillis()) + ".jpg");
Bitmap b = Bitmap.createScaledBitmap(bmp, 320, 480, false);
try {
FileOutputStream out = new FileOutputStream(myImage);
b.compress(Bitmap.CompressFormat.JPEG, 80, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
The code line you forgot is,,
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(myImage));
In your code, declare File myImage globally in your activity,
Now at Email send code
check whether file is exist or not,
if(myImage.exist())
{
String emailaddress[] = { "info#sklep.com", "", };
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL, emailaddress);
emailIntent.setType("image/jpeg");
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(myImage));
startActivity(Intent.createChooser(emailIntent, "Send Mail"));
}

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