支持其他应用打开,接收其他应用文件并保存

支持其他应用打开,接收其他应用文件并保存

一、需求

APP支持其他应用打开,使自己app出现在其他应用打开列表中。 APP接收第三方app文件,并保存在手机。

二、上代码

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)
                }
            }
        }
    }

猜你喜欢

转载自blog.csdn.net/weixin_44158429/article/details/124423436