My goal is to select a picture directly from phone's Gallery and use it in my app, like you would share it with other apps like WhatsApp, Mail, Messenger..etc etc.
I need to:
- Select the picture from gallery.
- Select my app from the share dialog.
- Launch a specific screen passing in the picture.
I've seen a lot of sharing packages but I'm not looking to load a picture by accessing the gallery from my app, but the other way around.
This package https://pub.dev/packages/receive_sharing_intent seems to do what I need but not in a very transparent and native looking way.. as instead of app icon and name, in the share dialog it shows the iOS extension created for this plugin..
In this post How do I share an image on iOS and Android using Flutter? I see Simon's answer could be the solution, but trying to implement it I'm getting quite a few errors after pasting the Kotlin sample code into MainActivity.kt
file. I guess it has to be updated.
This is my MainActivity.kt
file with added code from Simon's answer:
package com.vinny.fixit_cloud_biking
import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugins.GeneratedPluginRegistrant
class MainActivity: FlutterActivity() {
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine);
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
GeneratedPluginRegistrant.registerWith(this)
MethodChannel(flutterView,"channel:me.albie.share/share").setMethodCallHandler { methodCall, _ ->
if (methodCall.method == "shareFile") {
shareFile(methodCall.arguments as String)
}
}
}
private fun shareFile(path:String) {
val imageFile = File(this.applicationContext.cacheDir,path)
val contentUri = FileProvider.getUriForFile(this,"me.albie.share",imageFile)
val shareIntent = Intent()
shareIntent.action = Intent.ACTION_SEND
shareIntent.type="image/jpg"
shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri)
startActivity(Intent.createChooser(shareIntent,"Compartir usando"))
}
}
Errors onCreate()
:
Cannot access 'androidx.lifecycle.lifecycleOwner' which is a super type of 'com.vinny.fixit_cloud_biking.MainActivity'. Check your module class path for missing or conflicting dependencies.
onCreate: overrides nothing
Errors MethodChannel
:
Cannot access 'androidx.lifecycle.lifecycleOwner' which is a super type of 'io.flutter.embedding.android.FLutterActivity'. Check your module class path for missing or conflicting dependencies.
Unresolved reference: flutterView
As always many thanks for your help.