1、发送单个文件

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


2、发送多个文件

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



注意:本文归作者所有,未经作者允许,不得转载