How to activate “Share” button in android app?

后端 未结 4 827
我寻月下人不归
我寻月下人不归 2020-11-28 18:17

i want to add \"Share\" button to my android app.

Like that

\":\"

I added \"Share\" button, bu

4条回答
  •  暖寄归人
    2020-11-28 18:59

    Share Any File as below ( Kotlin ) :
    first create a folder named xml in the res folder and create a new XML Resource File named provider_paths.xml and put the below code inside it :

    
    
        
    
        
    
    

    now go to the manifests folder and open the AndroidManifest.xml and then put the below code inside the tag :

    
     // provider_paths.xml file path in this example
    
    

    now you put the below code in the setOnLongClickListener :

    share_btn.setOnClickListener {
        try {
            val file = File("pathOfFile")
            if(file.exists()) {
                val uri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".provider", file)
                val intent = Intent(Intent.ACTION_SEND)
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
                intent.setType("*/*")
                intent.putExtra(Intent.EXTRA_STREAM, uri)
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent)
            }
        } catch (e: java.lang.Exception) {
            e.printStackTrace()
            toast("Error")
        }
    }
    

提交回复
热议问题