Skip to content

Instantly share code, notes, and snippets.

@anka
Created November 27, 2019 15:51
Show Gist options
  • Save anka/b56d66dedce28de8c3c0c056d841f69b to your computer and use it in GitHub Desktop.
Save anka/b56d66dedce28de8c3c0c056d841f69b to your computer and use it in GitHub Desktop.
Android main activity implementing a Flutter method channel for file sharing
class MainActivity : FlutterActivity() {
private val SHARE_CHANNEL = "jademind.com/share"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
GeneratedPluginRegistrant.registerWith(this)
MethodChannel(flutterView, SHARE_CHANNEL).setMethodCallHandler { call, result ->
if (call.method.equals("share-file")) {
val argsMap = call.arguments as HashMap<String, String>
val subject = argsMap.get("subject")
val filename = argsMap.get("filename")
val filepath = argsMap.get("filepath")
val mimeType = argsMap.get("mimeType")
val contentUri = Uri.parse(filepath)
val shareIntent = Intent(Intent.ACTION_SEND)
shareIntent.setType(mimeType)
shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri)
this.startActivity(Intent.createChooser(shareIntent, subject))
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment