Android 实现文件分享功能(共享多个文件)
来源:互联网 发布:q币赚钱软件 编辑:程序博客网 时间:2024/06/06 08:23
效果如图:
神一样的代码:
针对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 实现文件分享功能(共享多个文件)
- Android 实现文件分享功能(共享多个文件)
- Android Intent分享文件|分享图片|分享文字|功能的实现
- struts实现多个文件上传功能
- AirDrop文件分享功能
- android实现文件上传功能
- 和其他App分享文件(1)设置文件共享
- android中实现多个apk文件。
- android中实现多个apk文件
- android中实现多个apk文件
- android中实现多个apk文件。
- Android实现多个apk文件安装
- 从多个文件中搜索指定单词功能实现
- java 实现多个文件打包成zip的功能
- android利用FTP实现文件共享
- iOS开发 实现原生分享 可以跳过选择面板,分享多个文件
- android 实现分享功能
- android实现分享功能
- Project Euler 45
- C#字符串相关函数(持续更新,绝不TJ)
- TCP大并发短连接造成TIME_WAIT问题解决办法
- 什么是回调函数
- spring源码阅读之IOC2 欢迎点评参考
- Android 实现文件分享功能(共享多个文件)
- HOJ 1446 Binomial Showdown
- 表格布局-允许被拉伸和收缩
- chrome开放工具的使用技巧;
- php实现冒泡排序
- Eclipse导入Android项目的正确方法及出错后的解决办法
- android开发小技巧2(Dx 1 error; aborting)
- 看操作系统神马的都弱爆了
- Yii框架的阅读,数据库的优化项