直播平台制作,支持其他应用打开,接收其他应用文件并保存

直播平台制作,支持其他应用打开,接收其他应用文件并保存

AndroidMainfest中

 

         <activity
            android:name=".ui.activity.OtherFileActivity"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>
                <data android:scheme="file"/>
                <data android:scheme="content"/>
                <data android:mimeType="*/*"/>  
                <data android:pathPattern="*/docx"/> 
            </intent-filter>
        </activity>
 

说明

 


OtherFileActivity 其他应用调用本app 打开的activity,也是接收数据的activity
android:mimeType=“/”
android:pathPattern=“*/docx”
保存Activity中接收的文件
    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        save(intent)
    }
    private fun save(intent: Intent?){
        val uri: Uri? = intent?.data
        val imageUri: Uri? = intent?.getParcelableExtra(Intent.EXTRA_STREAM)
        if (uri != null) {
            val scheme:String? = uri.scheme
            val host:String? = uri.host
            val port:Int = uri.port
            val path:String? = uri.path
            val query:String? = uri.query
            val action:String? = intent.action
            val type:String? = intent.type
            var content: String =""
            if (Intent.ACTION_SEND.equals(action) && type != null) {  //单文件
                if ("text/plain".equals(type)) {
                //TODO 单文本文件
                } else if (type.startsWith("image/")) {
                //TODO 单图片
                }
            } else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {  //多文件
                if (type.startsWith("image/")) {
                //TODO 图片列表
                }
            }
            var inputStream: InputStream? = null
            try {
                inputStream = contentResolver.openInputStream(uri)
                 content = Util.readStreamToString(inputStream)
                //content 就是读取到的内容了,请直接食用
            } catch (e: Exception) {
                e.printStackTrace()
            } finally {
                if (inputStream != null) {
                    try {
                        inputStream.close()
                    } catch (ignored: IOException) {
                    }
                }
            }
            content.let {
                val createFiles = File(this.filesDir, path)
                createFiles.exists()
                try {
                    createFiles.createNewFile()
                } catch (e: IOException) {
                    Log.d("TAG", "files err:" + e.message)
                }
            }
        }
    }

 

 

 以上就是 直播平台制作,支持其他应用打开,接收其他应用文件并保存,更多内容欢迎关注之后的文章

 

posted @ 2023-05-19 14:10  云豹科技-苏凌霄  阅读(9)  评论(0编辑  收藏  举报