public static Java.IO.File createTempFile(string filename, byte[] decrypted) { Java.IO.File tempFile = new Java.IO.File("/storage/emulated/0/jukebox/Songs/decrepted" + filename); FileOutputStream fos = new FileOutputStream(tempFile); tempFile.DeleteOnExit(); fos.Write(decrypted); fos.Close(); return(tempFile); }
public static FileDescriptor GetMediaFileDescriptor(Context context, byte[] byteArray) { try { // create temp file that will hold byte array Java.IO.File tempMp3 = Java.IO.File.CreateTempFile("demo", "mp4", context.CacheDir); tempMp3.DeleteOnExit(); FileOutputStream fos = new FileOutputStream(tempMp3); fos.Write(byteArray); fos.Close(); FileInputStream fis = new FileInputStream(tempMp3); return(fis.FD); } catch (Exception ex) { return(null); } }
public void ComposeEmail( IEnumerable <string> to, IEnumerable <string> cc = null, string subject = null, string body = null, bool isHtml = false, IEnumerable <EmailAttachment> attachments = null, string dialogTitle = null) { // http://stackoverflow.com/questions/2264622/android-multiple-email-attachments-using-intent var emailIntent = new Intent(Intent.ActionSendMultiple); if (to != null) { emailIntent.PutExtra(Intent.ExtraEmail, to.ToArray()); } if (cc != null) { emailIntent.PutExtra(Intent.ExtraCc, cc.ToArray()); } emailIntent.PutExtra(Intent.ExtraSubject, subject ?? string.Empty); body = body ?? string.Empty; if (isHtml) { emailIntent.SetType("text/html"); ICharSequence htmlBody; if (Build.VERSION.SdkInt >= BuildVersionCodes.N) { htmlBody = Html.FromHtml(body, FromHtmlOptions.ModeLegacy); } else #pragma warning disable CS0618 // Type or member is obsolete { htmlBody = Html.FromHtml(body); } #pragma warning restore CS0618 // Type or member is obsolete emailIntent.PutExtra(Intent.ExtraText, htmlBody); } else { emailIntent.SetType("text/plain"); emailIntent.PutExtra(Intent.ExtraText, body); } if (attachments != null) { var uris = new List <IParcelable>(); DoOnActivity(activity => { filesToDelete = new List <File>(); foreach (var file in attachments) { // fix for Gmail error using (var memoryStream = new MemoryStream()) { var fileName = Path.GetFileNameWithoutExtension(file.FileName); var extension = Path.GetExtension(file.FileName); // save file in external cache (required so Gmail app can independently access it, otherwise Gmail won't take the attachment) var newFile = new File(activity.ExternalCacheDir, fileName + extension); file.Content.CopyTo(memoryStream); var bytes = memoryStream.ToArray(); using (var localFileStream = new FileOutputStream(newFile)) { localFileStream.Write(bytes); } newFile.SetReadable(true, false); newFile.DeleteOnExit(); uris.Add(Uri.FromFile(newFile)); filesToDelete.Add(newFile); } } }); if (uris.Any()) { emailIntent.PutParcelableArrayListExtra(Intent.ExtraStream, uris); } } emailIntent.AddFlags(ActivityFlags.GrantReadUriPermission); emailIntent.AddFlags(ActivityFlags.GrantWriteUriPermission); // fix for GMail App 5.x (File not found / permission denied when using "StartActivity") StartActivityForResult(0, Intent.CreateChooser(emailIntent, dialogTitle ?? string.Empty)); }