Android 实现文件分享功能(共享多个文件)

效果如图:



 

神一样的代码:

针对image代码如下:

Intentshare=newIntent(Intent.ACTION_SEND); 

share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));//此处一定要用Uri.fromFile(file),其中file为File类型,否则附件无法发送成功。

share.setType("image/jpeg");   

startActivity(Intent.createChooser(share,"Share Image")); 

针对 text代码如下:

Intentshare=newIntent(Intent.ACTION_SEND); 

share.setType("text/plain"); 

share.putExtra(Intent.EXTRA_TEXT,"I'm being sent!!"); 

startActivity(Intent.createChooser(share,"Share Text")); 

 

 

//以下代码为我在应用中使用的      

Intent share = new Intent(Intent.ACTION_SEND);   
share.putExtra(Intent.EXTRA_STREAM, 
Uri.fromFile(file));

share.setType("*/*");//此处可发送多种文件

startActivity(Intent.createChooser(share, "Share"));

 

//共享多个文件代码如下

ArrayList<Uri> uris = new ArrayList<Uri>();

for(int i = 0; i < size; i++){
        File file=(File)list.get(selectedItemIndexes[i]).get("file");
        mimeType = getMIMEType(file);
        Uri u = Uri.fromFile(file);
        uris.add(u); 
      }
           boolean multiple = uris.size() > 1;
            Intent intent = new Intent(multiple ? android.content.Intent.ACTION_SEND_MULTIPLE
                    : android.content.Intent.ACTION_SEND);

            if (multiple) {
                intent.setType("*/*");
                intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
            } else {
                intent.setType(mimeType);
                intent.putExtra(Intent.EXTRA_STREAM, uris.get(0));
            }
            startActivity(Intent.createChooser(intent, "Share"));

 

 

如何在Android系统中发送带附件的电子邮件呢? 其实通过Intent可以很方便的发送Email,只需要短短10行代码就可以处理,这里Android开发网就以在sdcard上的android123.cwj文件为例,通过Intent来发送电子邮件。完整代码如下

File file = new File("\sdcard\android123.cwj"); //附件文件地址

 Intent intent = new Intent(Intent.ACTION_SEND);
 intent.putExtra("subject", file.getName()); //
 intent.putExtra("body", "android123 - email sender"); //正文
 intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file)); //添加附件,附件为file对象
            if (file.getName().endsWith(".gz")) {
                intent.setType("application/x-gzip"); //如果是gz使用gzip的mime
            } else if (file.getName().endsWith(".txt")) {
                intent.setType("text/plain"); //纯文本则用text/plain的mime
            } else {
                intent.setType("application/octet-stream"); //其他的均使用流当做二进制数据来发送
            }
  startActivity(intent); //调用系统的mail客户端进行发送

 

 

 

android2.3中,转发带附件的邮件时,新建的邮件中没有相应的附件。查看源码发现,确实没有相关实现,但貌似有实现的源码被注释掉了。求大体思路~~~~~

已解决 与大家分享一下:
/*******转发代码********/

  if (!loadAttachments(message, 0)) {
  mHandler.sendEmptyMessage(MSG_SKIPPED_ATTACHMENTS);
  }

  private boolean loadAttachments(Message message, int depth){
  try{
Attachment[] attachments = Attachment.restoreAttachmentsWithMessageId(this, message.mId);
for(final Attachment attachment:attachments){
mHandler.post(new Runnable() {
  public void run() {
  Uri aUri = Uri.parse(attachment.mContentUri);
  addAttachment(aUri);
  }
  });
}
return true;
  }catch(Exception ex){
  ex.printStackTrace();
  return false;
  }
  }

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值