简体   繁体   English

如何从 Kotlin 的内容意图中获取 uri 数据?

[英]How do you get uri data from content intent in Kotlin?

New to Android programming. Android 编程新手。 I'm trying to have my app open when an attachment that has my custom extension of ".replaysecret" is opened from whatsapp, messenger, gmail etc. The file is a pcm audio file I renamed with my extension.当从 whatsapp、messenger、gmail 等打开具有我的自定义扩展名“.replaysecret”的附件时,我试图打开我的应用程序。该文件是我用我的扩展名重命名的 pcm 音频文件。 My app is opening correctly and I can see the uri data and I found some code that gives me the file name but I can't figure out how to get the file path or load the file data some other way.我的应用程序正确打开,我可以看到 uri 数据,我找到了一些代码给我文件名,但我不知道如何获取文件路径或以其他方式加载文件数据。 Any suggestions on what I should be doing?关于我应该做什么的任何建议?

Here is my intent filter这是我的意图过滤器

        <intent-filter>
            <action android:name="android.intent.action.VIEW"/>

            <category android:name="android.intent.category.BROWSABLE"/>
            <category android:name="android.intent.category.DEFAULT"/>

            <data android:scheme="content"/>

            <data android:host="*"/>

            <!--  Required for Gmail and Samsung Email App  -->
            <data android:mimeType="application/octet-stream"/>

            <!--  Required for Outlook  -->
            <data android:mimeType="application/replaysecret"/>
        </intent-filter>

Here is my code for getting the file name from the uri这是我从 uri 获取文件名的代码

    var uri = intent.data

    if(uri != null){
        var fileName: String? = null
        var cursor = contentResolver.query(uri!!, null, null, null, null)
        if (cursor != null) {
            cursor.moveToFirst()
            fileName = cursor.getString(0)
            fileName = fileName.substring(fileName.lastIndexOf(":") + 1)
            cursor.close()
        }

       //Can't figure out how to get file path or load file data
    }

This is the uri string when opening the attachment if it matters: content://com.google.android.gm.sapi/myemail@gmail.com/message_attachment_external/%23thread-a%3Ar1178448143327120464/%23msg-a%3Ar8356029736797240583/0.1?account_type=com.google&mimeType=application&2Foctet-stream&rendition=1如果重要,这是打开附件时的 uri 字符串: content://com.google.android.gm.sapi/myemail@gmail.com/message_attachment_external/%23thread-a%3Ar1178448143327120464/%23msg-a%3Ar835602970316797 ?account_type=com.google&mimeType=application&2Foctet-stream&rendition=1

blackapps comment got me to the solution blackapps 评论让我找到了解决方案

    var uri = intent.data

    if(uri != null){
        var inputStream = this.contentResolver.openInputStream(uri)
        var byteArray = inputStream!!.readBytes()

        //do stuff with byteArray
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM