日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > Android >内容正文

Android

android开发 文件分享到应用,Android 实现文件分享功能(共享多个文件)

發(fā)布時(shí)間:2023/12/3 Android 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android开发 文件分享到应用,Android 实现文件分享功能(共享多个文件) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

效果如圖:

神一樣的代碼:

針對(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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。