Skip to content

Instantly share code, notes, and snippets.

@jaisonfdo
Created September 27, 2017 09:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaisonfdo/35a88daef24bdf1622c7dd997a8e20f6 to your computer and use it in GitHub Desktop.
Save jaisonfdo/35a88daef24bdf1622c7dd997a8e20f6 to your computer and use it in GitHub Desktop.
MailAttachment sample snippets
AssetManager assetManager = getAssets();
InputStream in = assetManager.open("sample.pdf");
public File stream2file(InputStream in) throws IOException
{
final File tempFile = File.createTempFile("sample1", ".pdf",
HomeActivity.this.getExternalCacheDir());
tempFile.deleteOnExit();
FileOutputStream out = new FileOutputStream(tempFile);
// for this you have to add the following dependency in your build.gradle
// compile 'org.apache.commons:commons-io:1.3.2'
IOUtils.copy(in, out);
return tempFile;
}
private void browseDocuments()
{
String[] mimeTypes =
{"application/msword", "text/plain", "application/pdf", "application/zip"};
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
intent.setType(mimeTypes.length == 1 ? mimeTypes[0] : "*/*");
if (mimeTypes.length > 0) {
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
}
} else {
String mimeTypesStr = "";
for (String mimeType : mimeTypes) {
mimeTypesStr += mimeType + "|";
}
intent.setType(mimeTypesStr.substring(0, mimeTypesStr.length() - 1));
}
startActivityForResult(Intent.createChooser(intent, "ChooseFile"), 100);
}
String mailID = "jaisonfdo@gmail.com";
String mailSubject = "Attachment Sample";
public static boolean isNetworkAvailable(Context context)
{
if (checkInternetConnection) {
ConnectivityManager connectivityManager = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting())
return true;
else {
if (showErrorMessage)
Toast.makeText(context, networkErrorMessage, Toast.LENGTH_SHORT).show();
return false;
}
} else
return true;
}
public static Boolean isAppAvailable(Context context, String appName)
{
PackageManager pm = context.getPackageManager();
boolean isInstalled;
try {
pm.getPackageInfo(appName,PackageManager.GET_ACTIVITIES);
isInstalled = true;
} catch (PackageManager.NameNotFoundException e) {
isInstalled = false;
}
return isInstalled;
}
public void sendMail(Context context, String mailID, String subject, File attachment, Uri uri)
{
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_EMAIL, mailID);
// Need to grant this permission
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
// Attachment
intent.setType("vnd.android.cursor.dir/email");
if (attachment != null)
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(attachment));
else if (uri != null)
intent.putExtra(Intent.EXTRA_STREAM, uri);
if (!TextUtils.isEmpty(subject))
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
if (isNetworkAvailable(context)) {
if (isAppAvailable(context, "com.google.android.gm"))
intent.setPackage("com.google.android.gm");
startActivityForResult(intent, 101);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment