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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

android 分享图片和视频

發(fā)布時(shí)間:2024/4/15 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 分享图片和视频 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

今天,簡(jiǎn)單講講如何調(diào)用手機(jī)的分享功能進(jìn)行分享。



目前實(shí)現(xiàn)一鍵分享功能的方式有兩種: 1.需要集成第三方官方SDK包,在獲得官方授權(quán)后調(diào)用其API來(lái)完成一鍵分享功能,例如使用友盟分享等 ? ? 優(yōu)點(diǎn):無(wú)縫集成,功能多 ? ? 缺點(diǎn):需要集成官方的SDK包并通過(guò)申請(qǐng)官方的授權(quán)才可進(jìn)行開(kāi)發(fā) 2.不需要使用任何第三方SDK包,可以直接調(diào)起實(shí)現(xiàn)了分享功能的應(yīng)用的activity來(lái)進(jìn)行分享 ? ? 優(yōu)點(diǎn):不需要使用任何第三方SDK包和申請(qǐng)官方授權(quán) ? ? 缺點(diǎn):需要手機(jī)安裝你需要分享的應(yīng)用(這一點(diǎn)非常重要,一開(kāi)始測(cè)試的時(shí)候一直不成功,提示“沒(méi)有應(yīng)用可執(zhí)行此操作”,后來(lái)找了很久才發(fā)現(xiàn)是我手機(jī)沒(méi)有安裝相對(duì)應(yīng)的應(yīng)用,這也是不好方便的地方)


效果圖:


package com.xiaolijuan.sharedome;import android.app.Activity; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.view.View; import android.widget.RelativeLayout;import java.io.File; import java.util.ArrayList;/*** 項(xiàng)目名稱:ShareDome* 類描述:* 創(chuàng)建人:xiaolijuan* 創(chuàng)建時(shí)間:2016/1/13 23:48*/ public class ShareActivity extends Activity implements View.OnClickListener {private RelativeLayout mRlShareText, mRlShareSingleimage, mRlShareMultipleimage, mRlShareQQ, mRlShareTencent, mRlShareWechat, mRlShareMicroblog, mRlShareOther;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_share);bindView();}private void bindView() {mRlShareText = (RelativeLayout) findViewById(R.id.rl_share_text);mRlShareSingleimage = (RelativeLayout) findViewById(R.id.rl_share_singleimage);mRlShareMultipleimage = (RelativeLayout) findViewById(R.id.rl_share_multipleimage);mRlShareQQ = (RelativeLayout) findViewById(R.id.rl_share_qq);mRlShareTencent = (RelativeLayout) findViewById(R.id.rl_share_qqtencent);mRlShareWechat = (RelativeLayout) findViewById(R.id.rl_share_wechat);mRlShareMicroblog = (RelativeLayout) findViewById(R.id.rl_share_microblog);mRlShareOther = (RelativeLayout) findViewById(R.id.rl_share_other);mRlShareText.setOnClickListener(new ShareText());mRlShareSingleimage.setOnClickListener(new ShareSingleImage());mRlShareMultipleimage.setOnClickListener(new ShareMultipleImage());mRlShareQQ.setOnClickListener(this);mRlShareTencent.setOnClickListener(this);mRlShareWechat.setOnClickListener(this);mRlShareMicroblog.setOnClickListener(this);mRlShareOther.setOnClickListener(this);}//分享文字至所有第三方軟件public class ShareText implements View.OnClickListener {@Overridepublic void onClick(View v) {Intent intent = new Intent();intent.setAction(Intent.ACTION_SEND);intent.putExtra(Intent.EXTRA_TEXT, "這里是分享內(nèi)容");intent.setType("text/plain");//設(shè)置分享列表的標(biāo)題,并且每次都顯示分享列表startActivity(Intent.createChooser(intent, "分享到"));}}//分享單張圖片至所有第三方軟件public class ShareSingleImage implements View.OnClickListener {@Overridepublic void onClick(View v) {String imagePath = Environment.getExternalStorageDirectory() + File.separator + "13e277bb0b9c2e3ab90229463357bf40.jpg";//由文件得到uriUri imageUri = Uri.fromFile(new File(imagePath));Intent shareIntent = new Intent();shareIntent.setAction(Intent.ACTION_SEND);shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);shareIntent.setType("image/*");startActivity(Intent.createChooser(shareIntent, "分享到"));}}//分享多張圖片至所有第三方軟件public class ShareMultipleImage implements View.OnClickListener {@Overridepublic void onClick(View v) {ArrayList<Uri> uriList = new ArrayList<>();String path = Environment.getExternalStorageDirectory() + File.separator;uriList.add(Uri.fromFile(new File(path+"13e277bb0b9c2e3ab90229463357bf40.jpg")));uriList.add(Uri.fromFile(new File(path+"869895e73ddd710e8a132afb37461bf0.jpg")));uriList.add(Uri.fromFile(new File(path+"4753fc4cd47aa1833c70df4c08f4b7fa.jpg")));uriList.add(Uri.fromFile(new File(path+"355ee87cf0ff612331a790f31b3ad113.jpg")));uriList.add(Uri.fromFile(new File(path+"ce61ad4d44e3099d87040f38faabbf56.jpg")));Intent shareIntent = new Intent();shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uriList);shareIntent.setType("image/*");startActivity(Intent.createChooser(shareIntent, "分享到"));}}@Overridepublic void onClick(View v) {String pakName = "";Intent intent = new Intent(Intent.ACTION_SEND); // 啟動(dòng)分享發(fā)送的屬性intent.setType("text/plain"); // 分享發(fā)送的數(shù)據(jù)類型switch (v.getId()) {case R.id.rl_share_qq:pakName = "com.qzone"; //qq空間break;case R.id.rl_share_qqtencent:pakName = "com.tencent.WBlog"; //騰訊微博break;case R.id.rl_share_wechat:pakName = "com.tencent.mm"; //微信break;case R.id.rl_share_microblog:pakName = "com.sina.weibo"; //微博break;case R.id.rl_share_other:break;default:break;}intent.setPackage(pakName);intent.putExtra(Intent.EXTRA_TEXT, "這里是分享內(nèi)容"); // 分享的內(nèi)容this.startActivity(Intent.createChooser(intent, ""));// 目標(biāo)應(yīng)用選擇對(duì)話框的標(biāo)題;} }


由于分享功能是使用隱式調(diào)用Activtiy實(shí)現(xiàn)的,則需在響應(yīng)的Activity中聲明intent-filter,在對(duì)應(yīng)的activity的xml里加上

<intent-filter><action android:name="android.intent.action.SEND" /><category android:name="android.intent.category.DEFAULT" /> </intent-filter>


這上面的代碼可以分享圖片,文本內(nèi)容到手機(jī)安裝的能分享的軟件,包括QQ,微信。但是,如果分享視頻時(shí),會(huì)發(fā)現(xiàn)無(wú)法分享到QQ里,這是因?yàn)橐曨l只能單個(gè)分享,一次只能分享一個(gè)視頻,所以需要修改代碼。

把分享圖片的

Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE); shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uriList); shareIntent.setType("image/*"); startActivity(Intent.createChooser(shareIntent, "分享到"));

修改為

Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND); //分享視頻只能單個(gè)分享 shareIntent.putExtra(Intent.EXTRA_STREAM, “視頻的URI”); shareIntent.setType("audio/*"); startActivity(Intent.createChooser(shareIntent, "分享到"));


源碼下載:http://download.csdn.net/detail/bzlj2912009596/9876105


android 分享圖片和視頻就講完了。


就這么簡(jiǎn)單。






總結(jié)

以上是生活随笔為你收集整理的android 分享图片和视频的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

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