Android系統(tǒng)自帶分享圖片功能
簡介
記錄一個(gè)利用系統(tǒng)分享功能進(jìn)行圖片分享的工具類(代碼是用Kotlin寫的,都是比較簡單的語法,部分可能需要自定義的地方都已經(jīng)標(biāo)出)。調(diào)用方式比較簡單:
Util.startShareImage(this) //this為當(dāng)前的Activity實(shí)例
權(quán)限
記得添加文件操作權(quán)限, 另外需要注意6.0版本以上的權(quán)限管理
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
具體細(xì)節(jié)見代碼
/**
* 系統(tǒng)分享圖片功能
* Created by wiky on 2018/1/13.
*/
object Util {
fun startShareImage(activity: Activity) {
//過濾出需要分享到對(duì)應(yīng)的平臺(tái):微信好友、朋友圈、QQ好友。 可自行修改
val targetApp = arrayOf("com.tencent.mm.ui.tools.ShareImgUI", "com.tencent.mm.ui.tools.ShareToTimeLineUI", "com.tencent.mobileqq.activity.JumpActivity")
/** * 分享圖片 */
val bitmap = getImageFromAssetsFile(activity, "img_share.jpg") //從assets目錄中取到對(duì)應(yīng)的文件,文件名自行修改
val localImage = saveBitmap(bitmap!!, "share.jpg") //分享前,需要先將圖片存在本地(記得添加權(quán)限),文件名自行修改
val shareIntent = Intent(Intent.ACTION_SEND)
shareIntent.type = "image/*" //設(shè)置分享內(nèi)容的類型:圖片
shareIntent.putExtra(Intent.EXTRA_STREAM, localImage)
try {
val resInfo = activity.packageManager.queryIntentActivities(shareIntent, 0)
if (!resInfo.isEmpty()) {
val targetedShareIntents = ArrayList<Intent>()
for (info in resInfo) {
val targeted = Intent(Intent.ACTION_SEND)
targeted.type = "image/*" //設(shè)置分享內(nèi)容的類型
val activityInfo = info.activityInfo
//如果還需要分享至其它平臺(tái),可以打印出具體信息,然后找到對(duì)應(yīng)的Activity名稱,填入上面的數(shù)組中即可
// println("package = ${activityInfo.packageName}, activity = ${activityInfo.name}")
//進(jìn)行過濾(只顯示需要分享的平臺(tái))
if (targetApp.any { it == activityInfo.name }) {
val comp = ComponentName(activityInfo.packageName, activityInfo.name)
targeted.component = comp
targeted.putExtra(Intent.EXTRA_STREAM, localImage)
targetedShareIntents.add(targeted)
}
}
val chooserIntent = Intent.createChooser(targetedShareIntents.removeAt(0), "選擇要分享到的平臺(tái)")
if (chooserIntent != null) {
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toTypedArray<Parcelable>())
activity.startActivity(chooserIntent)
}
}
} catch (e: Exception) {
Log.e(StatConstants.LOG_TAG, "Unable to share image, logs : " + e.toString())
}
}
/** * 從Assets中讀取圖片 */
private fun getImageFromAssetsFile(context: Context, fileName: String): Bitmap? {
var image: Bitmap? = null
val am = context.resources.assets
try {
val inputStream = am.open(fileName)
image = BitmapFactory.decodeStream(inputStream)
inputStream.close()
} catch (e: IOException) {
e.printStackTrace()
}
return image
}
/** * 將圖片存到本地 */
private fun saveBitmap(bm: Bitmap, picName: String): Uri? {
try {
val dir = Environment.getExternalStorageDirectory().absolutePath + File.separator + picName
val f = File(dir)
if (!f.exists()) {
f.parentFile.mkdirs()
f.createNewFile()
}
val out = FileOutputStream(f)
bm.compress(Bitmap.CompressFormat.JPEG, 90, out)
out.flush()
out.close()
return Uri.fromFile(f)
} catch (e: FileNotFoundException) {
e.printStackTrace()
} catch (e: IOException) {
e.printStackTrace()
}
return null
}
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Flutter利用注解生成可自定義的路由的實(shí)現(xiàn)
這篇文章主要介紹了Flutter利用注解生成可自定義的路由的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
Android實(shí)現(xiàn)圖片選擇上傳功能實(shí)例
這篇文章主要介紹了Android實(shí)現(xiàn)圖片選擇以及圖片上傳的功能,有需要的朋友跟著學(xué)習(xí)下吧。2017-12-12
Android下Activity間通信序列化過程中的深淺拷貝淺析
這篇文章主要給大家介紹了關(guān)于Android下Activity間通信序列化過程中深淺拷貝的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10
Android自動(dòng)測(cè)試工具M(jìn)onkey
Monkey是Android中的一個(gè)命令行工具,可以運(yùn)行在模擬器里或?qū)嶋H設(shè)備中。它向系統(tǒng)發(fā)送偽隨機(jī)的用戶事件流(如按鍵輸入、觸摸屏輸入、手勢(shì)輸入等),實(shí)現(xiàn)對(duì)正在開發(fā)的應(yīng)用程序進(jìn)行壓力測(cè)試。Monkey測(cè)試是一種為了測(cè)試軟件的穩(wěn)定性、健壯性的快速有效的方法2016-01-01
android開發(fā)教程之使用looper處理消息隊(duì)列
這篇文章主要介紹了通過HandlerThread對(duì)象來實(shí)現(xiàn)使用looper處理消息隊(duì)列的功能,大家參考使用吧2014-01-01
詳解Android數(shù)據(jù)存儲(chǔ)—使用SQLite數(shù)據(jù)庫
本篇文章主要介紹了詳解Android數(shù)據(jù)存儲(chǔ)—使用SQLite數(shù)據(jù)庫,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-03-03
Android自定義View實(shí)現(xiàn)等級(jí)滑動(dòng)條的實(shí)例
這篇文章主要介紹了 Android自定義View實(shí)現(xiàn)等級(jí)滑動(dòng)條的實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-04-04
Android實(shí)現(xiàn)有視差效果的ListView
這篇文章給大家詳解介紹了在Android中如何實(shí)現(xiàn)帶有視差效果的ListView,文章給出了示例代碼相信對(duì)大家的理解和學(xué)習(xí)更有幫助,有需要的朋友們下面來一起看看吧。2016-09-09

