Reputation: 11
Trying to test out a simple app that'll take an image from the gallery and pass it on as an attachment for an email, I've done the following:
Code for accessing gallery via ImageButton using intent
ivGalerija.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select content image"), 1);
}
});
OnActivityResult method overridden
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_OK){
if(requestCode == 1){
imageUri = data.getData();
File root = Environment.getExternalStorageDirectory();
String pathToMyAttachedFile = imageUri.getPath().toString();
File file = new File(root, pathToMyAttachedFile);
if (!file.exists() || !file.canRead()) {
return;
}
uri = Uri.fromFile(file);
}
}
}
onClick method on the submit button to open up the email app
bSubmit = (Button) findViewById(R.id.bSubmit);
bSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
etMesto = (EditText) findViewById(R.id.etMesto);
etUlica = (EditText) findViewById(R.id.etUlica);
etOpis = (EditText) findViewById(R.id.etOpis);
Intent intent = null, chooser = null;
es = (TextView) findViewById(R.id.tvMesto);
intent = new Intent(Intent.ACTION_SEND);
intent.setData(Uri.parse("mailto:"));
String[] to = {"[email protected]"};
intent.putExtra(Intent.EXTRA_EMAIL, to);
intent.putExtra(Intent.EXTRA_SUBJECT, "Prijava kvara");
intent.putExtra(Intent.EXTRA_TEXT, "Elektrodistribucija: " + spElektrodistribucija + "\nMesto neovlascene potrosnje: " +
etMesto.getText().toString() + "\nUlica i broj: " + etUlica.getText().toString() + "\nOpis: " + etOpis.getText().toString());
intent.setType("message/rfc822");
intent.putExtra(Intent.EXTRA_STREAM, uri);
chooser = Intent.createChooser(intent, "Send email");
startActivity(chooser);
}
});
}
The whole thing works fine without the attachment, text gets passed to the mail body correctly formatted, but there is no attachment. I've made the Uri field static to make it accessible everywhere, not sure if that's something that's where the problem lies. Anyway, without further ado... this code won't carry the image as an intent into the Email app and send it, instead the whole thing happens as if tho there's nothing selected. What am I missing?
Upvotes: 0
Views: 39
Reputation: 11224
Do not construct an uri from an uri.
intent.putExtra(Intent.EXTRA_STREAM, uri);
But use the original uri instead:
intent.putExtra(Intent.EXTRA_STREAM, data.getData());
Upvotes: 0