android开发 文件分享到应用,Android 实现文件分享功能(共享多个文件)
效果如圖:
神一樣的代碼:
針對(duì)image代碼如下:
Intentshare=newIntent(Intent.ACTION_SEND);
share.putExtra(Intent.EXTRA_STREAM,?Uri.fromFile(file));//此處一定要用Uri.fromFile(file),其中file為File類型,否則附件無法發(fā)送成功。
share.setType("image/jpeg");
startActivity(Intent.createChooser(share,"Share Image"));
針對(duì)?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"));
//以下代碼為我在應(yīng)用中使用的
Intent share = new Intent(Intent.ACTION_SEND);
share.putExtra(Intent.EXTRA_STREAM,
Uri.fromFile(file));
share.setType("*/*");//此處可發(fā)送多種文件
startActivity(Intent.createChooser(share, "Share"));
//共享多個(gè)文件代碼如下
ArrayList uris = new ArrayList();
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系統(tǒng)中發(fā)送帶附件的電子郵件呢? 其實(shí)通過Intent可以很方便的發(fā)送Email,只需要短短10行代碼就可以處理,這里Android開發(fā)網(wǎng)就以在sdcard上的android123.cwj文件為例,通過Intent來發(fā)送電子郵件。完整代碼如下
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對(duì)象
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"); //其他的均使用流當(dāng)做二進(jìn)制數(shù)據(jù)來發(fā)送
}
startActivity(intent); //調(diào)用系統(tǒng)的mail客戶端進(jìn)行發(fā)送
android2.3中,轉(zhuǎn)發(fā)帶附件的郵件時(shí),新建的郵件中沒有相應(yīng)的附件。查看源碼發(fā)現(xiàn),確實(shí)沒有相關(guān)實(shí)現(xiàn),但貌似有實(shí)現(xiàn)的源碼被注釋掉了。求大體思路~~~~~
已解決 與大家分享一下: /*******轉(zhuǎn)發(fā)代碼********/ ? 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; ? } ? }
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的android开发 文件分享到应用,Android 实现文件分享功能(共享多个文件)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux的用户分为三类(linux的用
- 下一篇: android 圆角按钮渐变,Andro