0

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:

  1. Select the picture from gallery.
  2. Select my app from the share dialog.
  3. 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.

Vincenzo
  • 5,304
  • 5
  • 38
  • 96
  • 1
    Does this answer your question? [How do I share an image on iOS and Android using Flutter?](https://stackoverflow.com/questions/44181343/how-do-i-share-an-image-on-ios-and-android-using-flutter) – Alok Aug 04 '20 at 19:33
  • @Alok Indeed that is the first post I found, but I'm not sure about the answers.. Apart from many answers suggesting their own plugin, there is Simon's answer which also uses `Share` plugin and also provides Swift and Kotlin code. Now my confusion: 1 Can I call question's `Dart` code from any Screen? eg. the package I linked in my question only handles intents in `main()`.. 2 where do I put Simon's answer `Kotlin` code? Bare with me..I's too new to Android and Flutter. Many thanks. – Vincenzo Aug 04 '20 at 19:51

0 Answers0