Intent.EXTRA_STREAM in Android - java

Why does it say String in Android documentation website, when EXTRA_STREAM is a URI. I'm trying to understand how to read android documentation.
EXTRA_STREAM
String EXTRA_STREAM
A content: URI holding a stream of data associated with the Intent, used with ACTION_SEND to supply the data being sent.
public void composeEmail(String[] addresses, String subject, Uri attachment) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_EMAIL, addresses);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_STREAM, attachment);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}

String is the type of the Constant Intent.EXTRA_STREAM, any extra name must be a String.

In information technology, a Uniform Resource Identifier is a string of characters used to identify a resource.

Related

Android send email with attachments using only email apps

The official docs show how you can send an email with an attachment:
public void composeEmail(String[] addresses, String subject, Uri attachment) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_EMAIL, addresses);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_STREAM, attachment);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
It then says:
If you want to ensure that your intent is handled only by an email app (and not other text messaging or social apps), then use the ACTION_SENDTO action and include the "mailto:" data scheme.
Like so:
public void composeEmail(String[] addresses, String subject) {
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:")); // only email apps should handle this
intent.putExtra(Intent.EXTRA_EMAIL, addresses);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
But actually, I want a combination of the above... i.e. send an email with an attachment and using only an email app.
But when using intent.setData(Uri.parse("mailto:")) in combination with Intent.ACTION_SEND or Intent.ACTION_SEND_MULTIPLE, nothing happens... no email app (or app chooser) opens at all.
So how do I send an email with attachment (or multiple attachments) whilst also restricting the app to email apps?
That mailto: URI string appears invalid without recipients; try something alike this instead:
intent.setData(Uri.parse("mailto:" + String.join(",", addresses)));
See RFC 6068: The 'mailto' URI Scheme.
Remove if (intent.resolveActivity(getPackageManager()) != null) check and it will open the intent. Usually the OS returns null whether the email handling apps exist or not.

Add attachment to gmail app via intent in Android

I have tried adding an attachment (csv file) to the mail intent. When the i start the intent and choose Gmail as the app, it says "Permission denied for the attachment". How do i solve this?
This is the code that i'm using
try {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("address", input.getText().toString());
editor.apply();
String gmail=sharedPreferences.getString("address","");
Uri dat = Uri.fromFile(path);
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/html");
sendIntent.putExtra(Intent.EXTRA_EMAIL, gmail);
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Stuff");
sendIntent.putExtra(Intent.EXTRA_STREAM, dat);
startActivity(Intent.createChooser(sendIntent, "Send email..."));
}catch (Exception e){
System.out.println(e.toString());
}
Your csv file is in private internal storage of your app. No other apps have access. Only your app. So the used email app has no access too. Put the file on another place. Or use a FileProvider.

Android ActivityNotFoundException failing to catch on Intent in OS 4.1.2 (works on 4.0.3)?

Thanks for taking a look. I am a very old programmer but absolute newbie to Android/Java. Although I haven't found a way to make code colorful and pretty here yet ;).
I am starting an activity to open a file with a bogus/fake extension (mime type) so I can force this ActivityNotFoundException exception (to test).
Somehow, this exception never happens on my Samsung Galaxy S3 (OS 4.1.2). Adobe Reader opens up and errors that the file is not a PDF (it's not). However on an older ASUS Transformer tablet running 4.0.3 the exception is caught properly.
The code in question is like so:
// this file exists (check omitted)..
File docFile = new File("/path/to/file.bogusextension");
// intent
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
// getting doc Uri
Uri fileUri = Uri.fromFile(docFile);
// getting doc mimeType
MimeTypeMap mime = MimeTypeMap.getSingleton();
String extension = MimeTypeMap.getFileExtensionFromUrl(fileUrl);
String mimeType = mime.getMimeTypeFromExtension(extension);
intent.setDataAndType(fileUri, mimeType);
// this is for an adobe native extension, just getting FREContext
// in the class below and mapping functions
DocLauncherExtensionContext extensionContext = (DocLauncherExtensionContext) context;
Activity activity = extensionContext.getActivity();
try
{
activity.startActivity(intent);
}
catch (ActivityNotFoundException e)
{
Toast.makeText(context.getActivity(), "This never happens!", Toast.LENGTH_LONG).show();
}
Is there anything blatent wrong with this code that would allow a file with any extension what-so-ever to be constantly thrown at Adobe Reader? I have cleared my defaults (Settings->Application Manager->Reset). Everything I startActivity is just hurled at Adobe Reader.
Any thoughts appreciated! Thank you!
Intent intent = new Intent();
needs to become:
Intent intent = new Intent(Intent.ACTION_VIEW);
You were putting data and an ActionCode into the intent, however you never told it what it was an intent for.

How to create a Uri from an Android resource with file extension

I am trying to send a png file in my res/drawable folder as an attachment in my email. I can get the file to attach and be included in the email, but it is missing the .png file extension. I want it to include the extension in the file name. Can someone help? Here is my code:
Intent i = new Intent(Intent.ACTION_SEND_MULTIPLE);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_SUBJECT, "Subject");
i.putExtra(Intent.EXTRA_TEXT , "Text");
ArrayList<Uri> uris = new ArrayList<Uri>();
Uri path = Uri.parse("android.resource://" + getPackageName() + "/" + R.drawable.png_image);
uris.add(newPath);
i.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
try {
startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(ShareActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
Change the type to this:
i.setType("image/png");
Although I believe putParcelableArrayListExtra() is not necessary. putExtra() should do the job.
Use this method to get the extension of the file
public static String getMimeType(Context context, Uri uri) {
String extension;
//Check uri format to avoid null
if (uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
//If scheme is a content
final MimeTypeMap mime = MimeTypeMap.getSingleton();
extension = mime.getExtensionFromMimeType(context.getContentResolver().getType(uri));
} else {
//If scheme is a File
//This will replace white spaces with %20 and also other special characters. This will avoid returning null values on file name with spaces and special characters.
extension = MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(new File(uri.getPath())).toString());
}
return extension;
}

How to send email newsletter from android application programmatically

I am trying to send an HTML table through email, but what i only get is a string with the HTML code.
I read about email newsletter a little but i didn't figure out how to make it work in my android test application.
I have a regular table that insert into String string.
String string = "<table border='1' align='center'><tr style='color:blue'><th>Day</th><th>Date</th><th>Start Time</th><th>End Time</th><th>Total Time</th></tr><tr><td align='center'>Sunday</td><td align='center'>19/07/2011</td><td align='center'>13:00</td><td align='center'>19:00</td><td align='center'>06:00</td></tr></table>";
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/html");
intent.putExtra(Intent.EXTRA_EMAIL, getSenderList());
intent.putExtra(Intent.EXTRA_SUBJECT, mContext.getText(R.string.app_name));
intent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(string));
mContext.startActivity(intent);
Someone can please point me how to do that?
Thanks
Check out the following question:
Send HTML mail using Android intent.
If you want to use the default mailer, change your code to the following:
String body = "<table border='1' align='center'><tr style='color:blue'><th>Day</th><th>Date</th><th>Start Time</th><th>End Time</th><th>Total Time</th></tr><tr><td align='center'>Sunday</td><td align='center'>19/07/2011</td><td align='center'>13:00</td><td align='center'>19:00</td><td align='center'>06:00</td></tr></table>";
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:" + getSenderList()));
intent.setType("text/html");
intent.putExtra(Intent.EXTRA_SUBJECT, mContext.getText(R.string.app_name));
intent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(body));
mContext.startActivity(intent);

Categories

Resources