首页 > 解决方案 > 如何使用意图过滤器将 gmail 附件放入本地设备商店

问题描述

In my application i want to open gmail attachment with my application, i used intent filter for that, 
    I am getting URI and i want to use that and copy that attachment to my local device and want to display that with my application .

    url = {content://com.google.android.gm.sapi/abc@gmail.com/message_attachment_external/%23thread-f%3A1610966348228029097/%23msg-f%3A1610966348228029097/0.1?account_type=com.google&mimeType=application%2Foctet-stream&rendition=1}

经过一天的研究和实施,这是 gmail 附件文件的最终解决方案,可以在我们的应用程序中查看和下载。

[IntentFilter(new string[] { Intent.ActionView }, Categories = new string[] { Intent.CategoryDefault, Intent.CategoryBrowsable }, DataScheme = "content", DataMimeType = "application/octet-stream")]

Stream inputsTream = resolver.OpenInputStream(url);
                        fs = File.Create(fileName);

                        byte[] buffer = new byte[32768];
                        int count;
                        int offset = 0;
                        while ((count = inputsTream.Read(buffer, offset, buffer.Length)) > 0)
                        {
                            fs.Write(buffer, 0, count);
                        }
                        fs.Close();
                        inputsTream.Close();

标签: xamarin.formsxamarin.android

解决方案


private fun deepLinkAcceptor(){
    var ins: InputStream? = null
    var os: FileOutputStream? = null
    var fullPath: String? = null
    var file: File;

    try {
        val action = intent.action
        if (Intent.ACTION_VIEW != action) {
            return
        }
        val uri = intent.data
        val scheme = uri!!.scheme
        var name: String? = null
      if (scheme == "content") {
            val cursor = contentResolver.query(
                uri!!, arrayOf(
                    MediaStore.MediaColumns.DISPLAY_NAME
                ), null, null, null
            )
            cursor!!.moveToFirst()
            val nameIndex = cursor!!.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME)
            if (nameIndex >= 0) {
                name = cursor!!.getString(nameIndex)
            }
        } else {
            return
        }

        if (name == null) {
            return
        }

        val n = name.lastIndexOf(".")
        val fileName: String
        val fileExt: String
        if (n == -1) {
            return
        } else {
            fileName = name.substring(0, n)
            fileExt = name.substring(n)
        }
        // create full path to where the file is to go, including name/ext
        fullPath = ""
        val filenm = fileName + fileExt
        file = File(cacheDir, filenm)
        ins = contentResolver.openInputStream(uri!!)
        os = FileOutputStream(file.getPath())
        val buffer = ByteArray(4096)
        var count: Int
        while (ins!!.read(buffer).also { count = it } > 0) {
            os!!.write(buffer, 0, count)
        }
        os!!.close()
        ins!!.close()

        println("===onfile path: " + file.getAbsolutePath())
        println("===onFile name: " + file.readText())
    } catch (e: Exception) {
        if (ins != null) {
            try {
                ins.close()
            } catch (e1: Exception) {
            }
        }
        if (os != null) {
            try {
                os.close()
            } catch (e1: Exception) {
            }
        }
        if (fullPath != null) {
            val f = File(fullPath)
            f.delete()
        }
    }
}

推荐阅读