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...")); 

希望这有助于;

相关问题