首页 > 其他分享 >Android开发 资源分享与被分享

Android开发 资源分享与被分享

时间:2022-09-29 12:37:04浏览次数:48  
标签:val SEND inputStream 开发 intent Intent Android 分享

前言

  在使用应用功能时会经常需要分享一些图片等等其他资源到微信上,或者接收一些其他应用分享来的图片等等其他资源。

接收分享来的资源

下面这个例子使用一个activity来接收分享来的图片,在AndroidManifest.xml 中添加, 请注意以下几个关键:

  • android:exported="true"  一定是true。否则此activity不会出现在分享弹窗的选项里
  • android.intent.action.SEND  为分享单个文件
  • android.intent.action.SEND_MULTIPLE  为分享多个文件
        <activity
            android:name=".ui.gallery.ReceiveShareImageActivity"
            android:exported="true" >
            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="image/*" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.SEND_MULTIPLE" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="image/*" />
            </intent-filter>
        </activity>

mimeType的其他类型

        <data android:mimeType="image/*" />
        <data android:mimeType="video/*" />
        <data android:mimeType="audio/*" />
        <data android:mimeType="text/x-vcard" />
        <data android:mimeType="text/x-vcalendar" />
        <data android:mimeType="text/calendar" />
        <data android:mimeType="text/plain" />
        <data android:mimeType="text/html" />
        <data android:mimeType="text/xml" />
        <data android:mimeType="application/zip" />
        <data android:mimeType="application/vnd.ms-excel" />
        <data android:mimeType="application/msword" />
        <data android:mimeType="application/vnd.ms-powerpoint" />
        <data android:mimeType="application/pdf" />
        <data android:mimeType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" />
        <data android:mimeType="application/vnd.openxmlformats-officedocument.wordprocessingml.document" />
        <data android:mimeType="application/vnd.openxmlformats-officedocument.presentationml.presentation" />
        <data android:mimeType="application/x-hwp" />

在activity的onCreate调用如下代码,获取分享来的图片:

    private fun getShareImage() {
        intent.type?.let { type ->
            /*
                单个图片分享接收
            */
            if (intent.action == Intent.ACTION_SEND && type.startsWith("image/")) {
                val uri: Uri? = intent.getParcelableExtra(Intent.EXTRA_STREAM)
                uri?.let { uri ->
                    val inputStream = contentResolver.openInputStream(uri)
                    val bitmap = BitmapFactory.decodeStream(inputStream)
                    inputStream?.close()
                }
            }
            /*
                多个图片分享接收
            */
            if (intent.action == Intent.ACTION_SEND_MULTIPLE && type.startsWith("image/")) {
                val arrayList: ArrayList<Uri>? =
                    intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM)
                arrayList?.let { arrayList ->
                    arrayList.forEach {
                        val inputStream = contentResolver.openInputStream(it)
                        val bitmap = BitmapFactory.decodeStream(inputStream)
                        inputStream?.close()
                    }
                }
            }
        }
    }

发送资源分享到其他应用

分享文字

    //分享文字
    fun shareText() {
        val shareIntent = Intent()
        shareIntent.action = Intent.ACTION_SEND
        shareIntent.putExtra(Intent.EXTRA_TEXT, "This is my Share text.")
        shareIntent.type = "text/plain"
        //设置分享列表的标题,并且每次都显示分享列表
        startActivity(Intent.createChooser(shareIntent, "分享到"))
    }

分享图片

 

 

End

 

标签:val,SEND,inputStream,开发,intent,Intent,Android,分享
From: https://www.cnblogs.com/guanxinjing/p/16741030.html

相关文章

  • uniapp 使用 uni.share分享功能报错 :fail:{"errMsg":"share:fail method 'share' not
    报错代码:<u-button:custom-style="customStyle"@click="share"><u-iconname="zhuanfa"color="#ffffff"size="35"></u-icon>分享</u-button>//分享s......
  • 刘潇翔:基于OpenHarmony的仿生四足狗开发分享
    刘潇翔:基于OpenHarmony的仿生四足狗开发分享​说起人工智能机器人界的网红,那就不得不提到——“机器狗”。此前,著名机器人公司波士顿动力制造的“Spot”机器狗、国内知名科......
  • android修改系统默认语言:
    //参考:http://t.zoukankan.com/x_wukong-p-9375264.htmlhttps://blog.csdn.net/qq_37858386/article/details/112616750https://news.68idc.cn/makewebs/asks/201404118......
  • .net开发平台
    .net开发平台下有三种开发框架.netframework(部分开源)适用于windows系统开发,其中里面包含asp.net  wpf  winform   支持Windows和Web应用程序。现在,您可......
  • UMLChina建模竞赛第3赛季第1轮-列举软件开发中的“遮羞布”
    ​​软件方法(下)分析和设计第8章连载[20210723更新]>>​​人为了遮掩某些容易被人批评的东西,会有意无意地做出一些事情来“遮羞”。例如,考生知道自己不努力学习,肯定考不好,可......
  • EFCore实体优先,模型驱动开发
    以下代码在”程序包管理控件器“窗口执行,且必须添加:Microsoft.EntityFrameworkCore.Tools包根据实体类生成迁移代码:add-migrationInitialCreate执行数据库迁移代码......
  • SSM整合功能模块开发
    pojo层:pojo表结构,在domain包中新建一个pojo类对用数据库中的表生成get和set及无参有参构造  dao层:   service层:接口  实现类  controller层......
  • 第一行代码——Android pdf
    高清扫描版下载链接:https://pan.baidu.com/s/11KAJX-VoSFc1DGptsHREpg点击这里获取提取码 ......
  • 【C语言】Visual Studio 2022开发环境搭建
    1.下载VisualStudio2022VisualStudio的官方网站:​​https://visualstudio.microsoft.com/​​点击下载VisualStudio社区版Community2.安装VisualStudio2022双击Visual......
  • Linux开发环境搭建与使用——Linux 目录结构及文件
    在windows平台下,打开“计算机”,我们看到的是一个个的盘符:在Linux下,我们是看不到这些盘符,我们看到的是文件夹(目录):在早期的UNIX系统中,各个厂家各自定义了自己的UNIX系......