Umit Kaya
Umit Kaya

Reputation: 5951

Attach imageView to email

I want to attach imageView to send through Android mail services. This is how i get image from Gallery in Activity A:

public class Activity A extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_gallery);

private static int RESULT_LOAD_IMAGE = 1;

Button viewcards=(Button)findViewById(R.id.viewcards);
viewcards.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);

}});      
}

@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]);
String picturePath = cursor.getString(columnIndex);
cursor.close();

Intent newdata = new Intent(SavedCards.this, Cardd.class);
newdata.putExtra("picture_path", picturePath);
startActivity(newdata); 
}

}}

Transfer image to another class and and put imageView in Activity B:

public class Activity B extends Activity {

private ImageView cardimage;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.carddd);

String temp = getIntent().getStringExtra("picture_path");
cardimage = (ImageView) findViewById(R.id.cardimage);
cardimage.setImageBitmap(BitmapFactory.decodeFile(temp));

and in Activity B, i want to attach this image into e-mail by onClick.

txtsend=(TextView) findViewById(R.id.txtsend);
txtsend.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {



}
});

As a reference, how image can attach e-mail is here, but this is to pick from gallery.

How to do this by taken image which is already set on imageView. Thank you.

Upvotes: 1

Views: 687

Answers (2)

Erzer
Erzer

Reputation: 86

In your code you setup image to ImageView from file in Activity B. And you know path to this picture file. When you send email just attach this file.

Uri uri = Uri.parse("file://" + attachmentFile); 
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);

Upvotes: 1

ranjk89
ranjk89

Reputation: 1570

You can use ImageView's getDrawable method see this http://developer.android.com/reference/android/widget/ImageView.html#getDrawable()

So something like,

Bitmap imgViewBmp = ((BitmapDrawable)cardImage.getDrawable()).getBitmap();

Should work.

PS: I would suggest you to first check the api reference docs for any class you want to use. Chances of finding what you are looking for is quite high. Next best thing would be to actually google what you want to solve, if that doesn't work then "rephrase" you words and try again. The chances that you will find your solution are doubled (if not more) with the latter!

Upvotes: 0

Related Questions