How to send the value of an android variable back to flutter?

Friends, I created a channel between flutter and android to take pictures without user action. And that already works!

I’m getting the image in base64 format. Now I need to get this variable value and send it to flutter. However, the way I’m doing it doesn’t work. I appreciate if anyone can help me!

Flutter

Here is my code: home.dart

.
.
class _HomePageState extends State<HomePage> {
  MethodChannel platform = Constants.platform;
  var isLoading = false;

  _HomePageState(){
    platform.setMethodCallHandler(_handleMethod);
  }

  Future<Null> _showNativeView() async {
    await platform.invokeMethod('showNativeView');
  }

  Future<dynamic> _handleMethod(MethodCall call) async {
    switch(call.method) {
      case "message":
        debugPrint(call.arguments);
        return new Future.value("");
    }
  }
.
.

constant.dart

import 'package:flutter/services.dart';

const platform = const MethodChannel('flutter.example.com/photo');

Android

MainActivity.java

package com.example.monitoramento;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;

import java.io.File;

import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugins.GeneratedPluginRegistrant;

public class MainActivity extends FlutterActivity {

    public static final String CHANNEL = "flutter.example.com/photo";
    public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
        super.configureFlutterEngine(flutterEngine);
        new MethodChannel(getFlutterEngine().getDartExecutor().getBinaryMessenger(),
                CHANNEL).setMethodCallHandler(new MethodChannel.MethodCallHandler() {
            @Override
            public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
                if(call.method.equals("showNativeView")){
                    Intent i = new Intent(MainActivity.this, DemoCamActivity.class);
                    startActivity(i);
                }else{
                    result.notImplemented();
                }
            }
        });
    }
}

The method of the DemoCamActivity.class class looks like this:

    public void onImageCapture(@NonNull File imageFile) {
        // Convert file to bitmap.
        // Do something.
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath(), options);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
        byte[] byteArray = byteArrayOutputStream.toByteArray();
        String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
        
        //Display the image to the image view
        ((ImageView) findViewById(R.id.cam_prev)).setImageBitmap(bitmap);

        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtra("message", encoded);
        startActivity(intent);
    }

I ran the code and the string is stored in the variable “encode”. I need to get this variable and send it back to flutter.

I appreciate any comments on how I can do this!

The code above doesn’t give an error, but it returns to the login page and not to home.dart.

1 Like