public void OpenFile(string filePath, string filename) { var bytes = System.IO.File.ReadAllBytes(filePath); //Copy the private file's data to the EXTERNAL PUBLIC location string externalStorageState = global::Android.OS.Environment.ExternalStorageState; string application = ""; string extension = System.IO.Path.GetExtension(filePath); switch (extension.ToLower()) { case ".doc": case ".docx": application = "application/msword"; break; case ".pdf": application = "application/pdf"; break; case ".xls": case ".xlsx": application = "application/vnd.ms-excel"; break; case ".jpg": case ".jpeg": case ".png": application = "image/jpeg"; break; default: application = "*/*"; break; } var externalPath = global::Android.OS.Environment.ExternalStorageDirectory.Path + "/" + filename + extension; System.IO.File.WriteAllBytes(externalPath, bytes); Java.IO.File file = new Java.IO.File(externalPath); file.SetReadable(true); //Android.Net.Uri uri = Android.Net.Uri.Parse("file://" + filePath); Android.Net.Uri uri = Android.Net.Uri.FromFile(file); Intent intent = new Intent(Intent.ActionView); intent.SetDataAndType(uri, application); intent.SetFlags(ActivityFlags.ClearWhenTaskReset | ActivityFlags.NewTask); try { Xamarin.Forms.Forms.Context.StartActivity(intent); } catch (Exception) { Toast.MakeText(Xamarin.Forms.Forms.Context, "There's no app to open dpf files", ToastLength.Short).Show(); } }
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)); }
void SendButtonClicked(object sender, EventArgs e) { if (saveHistory) { var history = new HistoryInformation (); foreach (var p in _temp_IMG_List) { history.IMG_List.Add (p); } foreach (var r in _temp_RCD_List) { history.RCD_List.Add (r); } history.latitude = _lat_TextView.Text; history.longitude = _long_TextView.Text; DataManager.SharedInstance.History.Add (history); } var email = new Intent (Android.Content.Intent.ActionSendMultiple); var uris = new List<IParcelable> (); _temp_IMG_List.ForEach (uri=>{ Java.IO.File file = new Java.IO.File (uri.Path); file.SetReadable(true,true); var u = Android.Net.Uri.FromFile(file); uris.Add(u); }); _temp_RCD_List.ForEach (uri =>{ var file = new Java.IO.File (uri.Path); file.SetReadable(true,true); var u = Android.Net.Uri.FromFile(file); uris.Add(u); }); email.PutParcelableArrayListExtra (Intent.ExtraStream,uris); var msg = string.Format (" http://maps.google.com/maps?z=12&t=m&q=loc:{0},{1}",_lat_TextView.Text,_long_TextView.Text); email.PutExtra (Android.Content.Intent.ExtraText,msg); email.SetType ("message/rfc822"); StartActivityForResult (email,2); }
static public void CopyBinaries() { string folder = Android.App.Application.Context.GetDir("Tor", FileCreationMode.WorldWriteable).AbsolutePath; string path = folder + "/tor"; if (System.IO.File.Exists(path)) { System.IO.File.Delete(path); } FileStream TorDest = System.IO.File.Create(path); using (Stream fs = MainActivity.assets.Open("tor")) { fs.CopyTo(TorDest); } TorDest.Close(); Java.IO.File fileTor = new Java.IO.File(folder + "/tor"); bool result = fileTor.SetExecutable(true); result = fileTor.SetReadable(true); result = fileTor.SetWritable(true); //Android.OS.file //Runtime.GetRuntime().Exec("chmod 755 "+path); //it works! //torrc path = folder + "/torrc"; // if (!System.IO.File.Exists(path)) //{ FileStream TorrcDest = System.IO.File.Create(path); using (Stream fs = MainActivity.assets.Open("torrc")) { fs.CopyTo(TorrcDest); } TorrcDest.Close(); // } Java.IO.File torrcfile = new Java.IO.File(folder + "/torrc"); result = torrcfile.SetReadable(true); result = torrcfile.SetWritable(true); StringBuilder text = new StringBuilder(); BufferedReader br = new BufferedReader(new FileReader(path)); System.String line; while ((line = br.ReadLine()) != null) { text.Append(line); text.Append('\n'); } br.Close(); if (text.IndexOf("/data/data/com.thanksoft.masksurfpro/Tor/data") != -1) { string Temp = text.ToString().Replace("/data/data/com.thanksoft.masksurfpro/Tor/data", folder + "/data"); text.Delete(0, text.Length()); text.Append(Temp); } if (text.IndexOf("/data/data/com.thanksoft.masksurfpro/Tor/geoip") != -1) { string Temp = text.ToString().Replace("/data/data/com.thanksoft.masksurfpro/Tor/geoip", folder + "/data"); text.Delete(0, text.Length()); text.Append(Temp); } if (text.IndexOf("/data/data/com.thanksoft.masksurfpro/Tor/geoip6") != -1) { string Temp = text.ToString().Replace("/data/data/com.thanksoft.masksurfpro/Tor/geoip6", folder + "/geoip6"); text.Delete(0, text.Length()); text.Append(Temp); } BufferedWriter bw = new BufferedWriter(new FileWriter(path)); bw.Write(text.ToString()); bw.Close(); //geoip path = folder + "/geoip"; if (!System.IO.File.Exists(path)) { FileStream GeoIPDest = System.IO.File.Create(path); using (Stream fs = MainActivity.assets.Open("geoip")) { fs.CopyTo(GeoIPDest); } GeoIPDest.Close(); } //geoip path = folder + "/geoip6"; if (!System.IO.File.Exists(path)) { FileStream GeoIP6Dest = System.IO.File.Create(path); using (Stream fs = MainActivity.assets.Open("geoip6")) { fs.CopyTo(GeoIP6Dest); } GeoIP6Dest.Close(); } //data folder path = folder + "data"; if (!System.IO.Directory.Exists(path)) { System.IO.Directory.CreateDirectory(path); } //Polipo path = folder + "/polipo"; if (!System.IO.File.Exists(path)) { FileStream PolipoDest = System.IO.File.Create(path); using (Stream fs = MainActivity.assets.Open("polipo")) { fs.CopyTo(PolipoDest); } PolipoDest.Close(); Java.IO.File filePolipo = new Java.IO.File(path); filePolipo.SetExecutable(true); } path = folder + "/polipo.conf"; if (!System.IO.File.Exists(path)) { FileStream PolipoConfDest = System.IO.File.Create(path); using (Stream fs = MainActivity.assets.Open("polipo.conf")) { fs.CopyTo(PolipoConfDest); } PolipoConfDest.Close(); } }