1

我想用Android intent發送.png文件。我曾嘗試使用WorldReadable權限在內部存儲器中保存此文件,現在已保存到外部存儲器。當我打開GMail客戶端時,我的附件就在那裏。但是,在Microsoft Exchange或Outlook應用程序中,附件不會顯示,我必須手動添加它。意向發送電子郵件附件android。只適用於Gmail

我使用Xamarin.Android(MonoDroid),但任何Java解決方案也會有所幫助。 這是我的意圖發送電子郵件的代碼。

 Intent intent = new Intent(Intent.ACTION_SENDTO); 
     intent.setType("image/png"); // I also tried 'application/image' 
     intent.setData(Android.Net.Uri.Parse("mailto:" + address ?? string.Empty)); 

     if (!string.isNullOrEmpty (attachment)) { 
      intent.putExtra (Android.Content.Intent.EXTRA_STREAM, Android.Net.Uri.fromFile(new Java.IO.File (Android.OS.Environment.getExternalStorageDirectory(), attachment))); 
     } 

     try { 
      _Context.startActivity(intent); 
     }catch(ActivityNotFoundException anfe) { 

      //Show prompt 
     } 

我不知道爲什麼附件只顯示在GMail中。我是否需要內容提供商?奇怪的是,它只出現在GMail中,而不是任何其他郵件應用程序。

回答

2

你不應該需要一個ContentProvider。

我在做基本上相同的事情發送JPEG,但使用Intent.ActionSend而不是Intent.ActionSendTo

+0

除了意圖設置爲Intent.ActionSend我不得不改變的電子郵件地址intent.PutExtra(Intent.ExtraEmail),「 [email protected]'); – kevskree

0
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) 
    { 
     bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.cstand); 
     File sdCard = Environment.getExternalStorageDirectory(); 
     System.out.println("PATH : "+sdCard); 
     File dir = new File(sdCard.getAbsolutePath() + "/Salary/Documents/Email");//#2 

     dir.mkdirs(); 
     File file = new File(dir, "Document.png"); 

     try 
     { 
      output = new FileOutputStream(file); 
      bitmap.compress(Bitmap.CompressFormat.PNG, 100, output); 
      output.flush(); 
      output.close(); 
      String subject = "Document" ; 
      String message = "Please, see the attcahed document"; 

      //FOR EMAIL 
      Intent email = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", "", null)); 
      email.putExtra(Intent.EXTRA_SUBJECT, subject); 
      Uri uri = Uri.fromFile(file); 
      email.putExtra(Intent.EXTRA_STREAM, uri); 
      email.putExtra(Intent.EXTRA_TEXT, message); 
      startActivity(Intent.createChooser(email, "Choose any Email:")); 

      } 
     catch (android.content.ActivityNotFoundException ex) 
     { 
      Toast.makeText(MainActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show(); 
     } 
     catch (FileNotFoundException e) 
     { 
      e.printStackTrace(); 
     } 
     catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
+0

你能否也添加一些上下文/解釋? – Robert

+0

U需要Manifest中的權限<使用權限android:name =「android.permission.WRITE_EXTERNAL_STORAGE」/>如果圖像位於SDCARD中,則只能將圖像附加到郵件。因此,您需要將圖像複製到SD或在飛行中創建並附加。 \t 我剛試過,它對我有用,希望它有幫助!請接受,如果它:) – user2239842

0

嘗試使用ActionSendMultipleIntent:Intent(Intent.ActionSendMultiple)。對於附件使用PutParcelableArrayListExtra(Intent.ExtraStream, uris)其中uris變量是List<IParcelable>()

下面是一個例子:

var email = new Intent(Intent.ActionSendMultiple); 
    email.SetType("text/plain"); 
    email.PutExtra(Intent.ExtraEmail, new string[]{emailTo}); 
    email.PutExtra(Intent.ExtraCc, new string[]{emailCC}); 

    var uris = new List<IParcelable>(); 
    filePaths.ForEach(file=> { 
     var fileIn = new File(file); 
     var uri = Android.Net.Uri.FromFile(fileIn); 
     uris.Add(uri); 
    }); 

    email.PutParcelableArrayListExtra(Intent.ExtraStream, uris); 

    context.StartActivity(Intent.CreateChooser(email, "Send mail...")); 

希望這有助於;

相關問題