android 分享文件到应用,安卓-代码--打开和分享(发送到)本地文件

打开不同类型的文件

http://codego.net/588405/ 文件管理

http://android.tgbus.com/Android/tutorial/201204/422728.shtml

http://dengzhangtao.iteye.com/blog/1946941

http://blog.csdn.net/yuxiaohui78/article/details/8232402

查看(打开)

/**

* 方法说明:用法 startActivity(Utils.openFile(savePath, "*"));

*

* @author Aotu-JS ,email: dev@jiangjiesheng.cn

* @version 创建时间:2016年6月22日 下午6:33:29

*

* @param localFilePath

* @return

*/

public static Intent openFile(String localFilePath, String fileType) {

Intent intent = new Intent("android.intent.action.VIEW");

intent.addCategory("android.intent.category.DEFAULT");

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Uri uri = Uri.fromFile(new File(localFilePath));

String lowerCasePath = localFilePath.toLowerCase();

// intent.setDataAndType(uri, "application/" + fileType);// msword

if (lowerCasePath.endsWith(".jpg") || lowerCasePath.endsWith(".png")

|| lowerCasePath.endsWith(".jpeg"))

fileType = "image/*";

else if (lowerCasePath.endsWith(".doc")

|| lowerCasePath.endsWith(".docx"))

fileType = "application/msword";

else if (lowerCasePath.endsWith(".pdf"))

fileType = "application/pdf";

else if (lowerCasePath.endsWith(".xls")

|| lowerCasePath.endsWith(".xlsx"))

fileType = "application/vnd.ms-excel";

else

fileType = "application/*";

intent.setDataAndType(uri, fileType);// msword

return intent;

}

/**

* 方法说明:(通过第三方软件)发送到(分享到)

*

* @author Aotu-JS ,email: dev@jiangjiesheng.cn

* @version 创建时间:2016年6月23日 下午1:45:57

*

* @param context

* @param activityTitle

* @param msgTitle

* @param msgText

* @param filePath

*/

public static void shareFileTo(Activity act, String activityTitle,

String msgTitle, String msgText, String filePath) {

if (null == act) {

return;

}

Intent intent = new Intent(Intent.ACTION_SEND);

if (filePath == null || filePath.equals("")) {

intent.setType("text/plain"); // 纯文本

} else {

File f = new File(filePath);

if (f != null && f.exists() && f.isFile()) {

String fileType;

String lowerCasePath = filePath.toLowerCase();

if (lowerCasePath.endsWith(".jpg")

|| lowerCasePath.endsWith(".png")

|| lowerCasePath.endsWith(".jpeg"))

fileType = "image/*";

else if (lowerCasePath.endsWith(".doc")

|| lowerCasePath.endsWith(".docx"))

fileType = "application/msword";

else if (lowerCasePath.endsWith(".pdf"))

fileType = "application/pdf";

else if (lowerCasePath.endsWith(".xls")

|| lowerCasePath.endsWith(".xlsx"))

fileType = "application/vnd.ms-excel";

else

fileType = "application/*";

intent.setType(fileType);

Uri u = Uri.fromFile(f);

intent.putExtra(Intent.EXTRA_STREAM, u);

}

}

intent.putExtra(Intent.EXTRA_SUBJECT, msgTitle);

ArrayList extra_text = new ArrayList();

extra_text.add(msgText);

intent.putStringArrayListExtra(android.content.Intent.EXTRA_TEXT, extra_text);

//intent.putExtra(Intent.EXTRA_TEXT, msgText);//170406-js-会有警告 4.x系统的bug,

//使用putStringArrayListExtra代替

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);//解决第二次分享到qq失败

act.startActivity(Intent.createChooser(intent, activityTitle));

}

/**

* 方法说明:(通过第三方软件)发送到(分享到) 支持多个文件

* @author 江节胜 ,E-mail:dev@jiangjiesheng.cn

* @version 创建时间:2017-4-1 下午4:48:39

*

* @param context

* @param activityTitle

* @param msgTitle

* @param msgText

* @param filePathList

*/

public static void shareFileTo(Activity act, String activityTitle,

String msgTitle, String msgText, List filePathList) {

if (null == act) {

return;

}

if (filePathList == null || filePathList.size() == 0) {

return;

}

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

ArrayList uris = new ArrayList();

for (int i = 0; i < filePathList.size(); i++) {

File f = new File(filePathList.get(i));

/* mimeType = getMIMEType(f); */

Uri u = Uri.fromFile(f);

uris.add(u);

}

boolean multiple = uris.size() > 1;

Intent intent = new Intent(multiple ? Intent.ACTION_SEND_MULTIPLE

: Intent.ACTION_SEND);

if (multiple) {

intent.setType("*/*");

intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);

} else {

String fileType;

String lowerCasePath = filePathList.get(0).toLowerCase();

if (lowerCasePath.endsWith(".jpg")

|| lowerCasePath.endsWith(".png")

|| lowerCasePath.endsWith(".jpeg"))

fileType = "image/*";

else if (lowerCasePath.endsWith(".doc")

|| lowerCasePath.endsWith(".docx"))

fileType = "application/msword";

else if (lowerCasePath.endsWith(".pdf"))

fileType = "application/pdf";

else if (lowerCasePath.endsWith(".xls")

|| lowerCasePath.endsWith(".xlsx"))

fileType = "application/vnd.ms-excel";

else

fileType = "application/*";

intent.setType(fileType);

intent.putExtra(Intent.EXTRA_STREAM, uris.get(0));

}

intent.putExtra(Intent.EXTRA_SUBJECT, msgTitle);

ArrayList extra_text = new ArrayList();

extra_text.add(msgText);

intent.putStringArrayListExtra(android.content.Intent.EXTRA_TEXT, extra_text);

//intent.putExtra(Intent.EXTRA_TEXT, msgText);//170406-js-会有警告 4.x系统的bug,

//使用putStringArrayListExtra代替

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);//解决第二次分享到qq失败

act.startActivity(Intent.createChooser(intent, activityTitle));

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值