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

canca11年前 (2013-11-03)Android286

神一样的代码:

针对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;
  }
  }

相关文章

Android 使用 adb命令 远程安装apk

./adb devices 列出所有设备./adb connect 192.168.1.89 连接到该设备./adb disconnect...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。