Flutter 腾讯云COS上传文件 使用andriod flutter混合开发 使用flutter1.12 1.17及以上版本

flutter 腾讯云COS上传 使用andriod flutter混合开发 试用flutter1.12 1.17及以上版本

  • flutter 腾讯云COS上传 使用andriod flutter混合开发 使用flutter1.12 1.17及以上版本
        • 前言
        • 解决方法 andriod与flutter混合开发
          • 推荐大家用as打开flutter目录下的app目录编写 flutter环境下java会红线报错
          • 打开后的目录如图 一开始打开要下载各种依赖 很慢 多等一会
        • Andriod代码部分
          • CosFlutterPlugin.java
          • GeneratedPluginRegistrant.java
          • MainActivity.java
        • flutter调用的代码
        • 参考的官方文档

flutter 腾讯云COS上传 使用andriod flutter混合开发 使用flutter1.12 1.17及以上版本

前言

腾讯云对Flutter极不友好 在其官方文档里没有给出flutter的官方插件 flutter插件网https://pub.dev/里唯一的插件经我反复尝试已经不能使用。现研究混合开发,又发现新的flutter版本(>1.12)已经不兼容旧的了,所以需要新的api接口。

解决方法 andriod与flutter混合开发

在网上搜索了很多混合开发的,基本处于在andriod基础上进行flutter的增加。这不是我们的目的,我们的目的是在flutter的基础上编写andriod代码。
大家最好先在博客里看一看混合开发,学一学基础,再进行相应的混合开发的代码编写。

推荐大家用as打开flutter目录下的app目录编写 flutter环境下java会红线报错

如图所示的app
Flutter 腾讯云COS上传文件 使用andriod flutter混合开发 使用flutter1.12 1.17及以上版本_第1张图片

打开后的目录如图 一开始打开要下载各种依赖 很慢 多等一会

MyFlutterPlugin那个不用在意 是test用 其他的代码贴在下方
Flutter 腾讯云COS上传文件 使用andriod flutter混合开发 使用flutter1.12 1.17及以上版本_第2张图片

Andriod代码部分

CosFlutterPlugin.java
package io.flutter.plugins;

import android.app.Activity;

import com.tencent.cos.xml.CosXmlService;
import com.tencent.cos.xml.CosXmlServiceConfig;
import com.tencent.cos.xml.exception.CosXmlClientException;
import com.tencent.cos.xml.exception.CosXmlServiceException;
import com.tencent.cos.xml.listener.CosXmlProgressListener;
import com.tencent.cos.xml.listener.CosXmlResultListener;
import com.tencent.cos.xml.model.CosXmlRequest;
import com.tencent.cos.xml.model.CosXmlResult;
import com.tencent.cos.xml.transfer.COSXMLUploadTask;
import com.tencent.cos.xml.transfer.TransferConfig;
import com.tencent.cos.xml.transfer.TransferManager;
import com.tencent.qcloud.core.auth.QCloudCredentialProvider;
import com.tencent.qcloud.core.auth.ShortTimeCredentialProvider;
import java.util.HashMap;
import io.flutter.Log;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.PluginRegistry;

import android.content.Context;
import android.app.Application;
import androidx.annotation.NonNull;
import java.lang.ref.WeakReference;
import java.util.UUID;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import static com.amap.api.maps.model.BitmapDescriptorFactory.getContext;

public final class CosFlutterPlugin implements FlutterPlugin, MethodChannel.MethodCallHandler, ActivityAware {
    private static Context mContext;
    private static MethodChannel channel;
    private static final String PLUGIN_NAME = "tencent_cos";
    private Application mApplication;
    private Activity mActivity;

    @Override
    public void onMethodCall(MethodCall call, MethodChannel.Result result) {
        if (call.method.equals("TencentCos.uploadFile")) {

            Log.e("TencentCosPlugin", "TencentCos.uploadFile");

            String secretId = "xxxxxxxxxxxxxxxxx"; //永久密钥 secretId 改为自己的
            String secretKey = "xxxxxxxxxxxxxxxxx"; //永久密钥 secretKey

// keyDuration 为请求中的密钥有效期,单位为秒
            QCloudCredentialProvider myCredentialProvider =
                    new ShortTimeCredentialProvider(secretId, secretKey, 300);

            String region = "xxxxxxxxxxxxxxxxx";//改为自己的
            String bucket = "xxxxxxxxxxxxxxxxx";//改为自己的

            final String localPath = call.argument("localPath");
            final String way = call.argument("way");

            android.util.Log.d(localPath, "onMethodCall: ");
            // 先查找
            int index = localPath.lastIndexOf(".");
            // 截取
            String lastname = localPath.substring(index, localPath.length());
            //生成uuid
            UUID own=UUID.randomUUID();
            String cosPath = "app/"+way+"/20."+own.toString().replace("-", "")+lastname;
            /// 初始化 COS Service
            // 创建 CosXmlServiceConfig 对象,根据需要修改默认的配置参数
//            CosXmlServiceConfig.Builder builder = new CosXmlServiceConfig.Builder().setRegion(region).setDebuggable(false).isHttps(true);
            CosXmlServiceConfig serviceConfig = new CosXmlServiceConfig.Builder()
                    .setRegion(region)
                    .isHttps(true) // 使用 HTTPS 请求, 默认为 HTTP 请求
                    .builder();
            // 初始化 COS Service,获取实例
//            context = InstrumentationRegistry.getInstrumentation().getTargetContext();
            CosXmlService cosXmlService = new CosXmlService(mContext, serviceConfig, myCredentialProvider);

            ///访问 COS 服务
            // 初始化 TransferConfig,这里使用默认配置,如果需要定制,请参考 SDK 接口文档
            TransferConfig transferConfig = new TransferConfig.Builder().build();
            //初始化 TransferManager
            TransferManager transferManager = new TransferManager(cosXmlService, transferConfig);
            //上传文件
            COSXMLUploadTask cosxmlUploadTask = transferManager.upload(bucket, cosPath, localPath, null);

            final HashMap<String, Object> data = new HashMap<>();
            data.put("localPath", localPath);
            data.put("cosPath", cosPath);
//            result.success("https://kt-1301681474.cos.ap-shanghai.myqcloud.com/"+cosPath);
            cosxmlUploadTask.setCosXmlProgressListener(new CosXmlProgressListener() {
                @Override
                public void onProgress(final long complete, final long target) {
                    mActivity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
//                            result.success( complete + "====run====" + target);
                            channel.invokeMethod("onProgress", complete + "====run====" + target);
                        }
                    });
//                    Log.e("onProgress", cosPath);
//                    Log.d("TencentCosPlugin", "onProgress =${progress * 100.0 / max}%");
                }
            });
            //设置返回结果回调
            cosxmlUploadTask.setCosXmlResultListener(new CosXmlResultListener() {
                @Override
                public void onSuccess(CosXmlRequest request, CosXmlResult httPesult) {
//                    COSXMLUploadTask.COSXMLUploadTaskResult cOSXMLUploadTaskResult =
//                            (COSXMLUploadTask.COSXMLUploadTaskResult) result;
                    Log.d("onSuccess", cosPath);
                    Log.d("TencentCosPlugin", "Success: " + httPesult.printResult());
                    mActivity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            result.success("https://kt-1301681474.cos.ap-shanghai.myqcloud.com/"+cosPath);
                        }
                    });
//                    result.success("0");
                }

                @Override
                public void onFail(CosXmlRequest request, CosXmlClientException exception, CosXmlServiceException serviceException) {
                    Log.d("TencentCosPlugin", "Failed: " + (exception.toString() + serviceException.toString()));
                    data.put("message", (exception.toString() + serviceException.toString()));
                    mActivity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            result.success("fail upload");
                        }
                    });

                    if (exception != null) {
                        exception.printStackTrace();
                    } else {
                        serviceException.printStackTrace();
                    }
                }
            });
        } else {
            result.notImplemented();
        }
    }


//    public CosFlutterPlugin(PluginRegistry.Registrar registrar, MethodChannel channel) {
//        this.registrar = registrar;
//        this.channel = channel;
//    }
//
//    /**
//     * Plugin registration. 旧方法
//     */
//    public static void registerWith(PluginRegistry.Registrar registrar) {
//        final MethodChannel channel = new MethodChannel(registrar.messenger(), "tencent_cos");
//        channel.setMethodCallHandler(new CosFlutterPlugin(registrar, channel));
//    }

    //// FlutterPlugin 的两个 方法
    @Override
    public void onAttachedToEngine(@NonNull FlutterPlugin.FlutterPluginBinding binding) {

        Log.e("onAttachedToEngine", "onAttachedToEngine");
        channel = new MethodChannel(binding.getBinaryMessenger(), PLUGIN_NAME);
        mApplication = (Application) binding.getApplicationContext();
        mContext=binding.getApplicationContext();
        channel.setMethodCallHandler(this);
    }

    @Override
    public void onDetachedFromEngine(@NonNull FlutterPlugin.FlutterPluginBinding binding) {

        Log.e("onDetachedFromEngine", "onDetachedFromEngine");

        channel.setMethodCallHandler(null);
        channel = null;
    }


    ///activity 生命周期
    @Override
    public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {

        Log.e("onAttachedToActivity", "onAttachedToActivity");

        mActivity = binding.getActivity();
    }

    @Override
    public void onDetachedFromActivityForConfigChanges() {
        Log.e("onDetachedFromActivityForConfigChanges", "onDetachedFromActivityForConfigChanges");
    }

    @Override
    public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) {
        Log.e("onReattachedToActivityForConfigChanges", "onReattachedToActivityForConfigChanges");
    }

    @Override
    public void onDetachedFromActivity() {
        Log.e("onDetachedFromActivity", "onDetachedFromActivity");

        mActivity = null;
    }
}
GeneratedPluginRegistrant.java
package io.flutter.plugins;

import androidx.annotation.Keep;
import androidx.annotation.NonNull;

import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.plugins.shim.ShimPluginRegistry;

/**
 * Generated file. Do not edit.
 * This file is generated by the Flutter tool based on the
 * plugins that support the Android platform.
 */
@Keep
public final class GeneratedPluginRegistrant {
  public static void registerWith(@NonNull FlutterEngine flutterEngine) {
    ShimPluginRegistry shimPluginRegistry = new ShimPluginRegistry(flutterEngine);
      me.yohom.amap_all_fluttify.AmapAllFluttifyPlugin.registerWith(shimPluginRegistry.registrarFor("me.yohom.amap_all_fluttify.AmapAllFluttifyPlugin"));
    flutterEngine.getPlugins().add(new me.yohom.amap_core_fluttify.AmapCoreFluttifyPlugin());
    flutterEngine.getPlugins().add(new me.yohom.amap_location_fluttify.AmapLocationFluttifyPlugin());
    flutterEngine.getPlugins().add(new me.yohom.amap_map_fluttify.AmapMapFluttifyPlugin());
    flutterEngine.getPlugins().add(new me.yohom.amap_search_fluttify.AmapSearchFluttifyPlugin());
    flutterEngine.getPlugins().add(new me.yohom.core_location_fluttify.CoreLocationFluttifyPlugin());
    flutterEngine.getPlugins().add(new io.flutter.plugins.deviceinfo.DeviceInfoPlugin());
      io.flutter.plugins.flutter_plugin_android_lifecycle.FlutterAndroidLifecyclePlugin.registerWith(shimPluginRegistry.registrarFor("io.flutter.plugins.flutter_plugin_android_lifecycle.FlutterAndroidLifecyclePlugin"));
    flutterEngine.getPlugins().add(new io.github.ponnamkarthik.toast.fluttertoast.FlutterToastPlugin());
    flutterEngine.getPlugins().add(new me.yohom.foundation_fluttify.FoundationFluttifyPlugin());
    flutterEngine.getPlugins().add(new io.flutter.plugins.imagepicker.ImagePickerPlugin());
      com.vansz.loading_indicator_view.LoadingIndicatorViewPlugin.registerWith(shimPluginRegistry.registrarFor("com.vansz.loading_indicator_view.LoadingIndicatorViewPlugin"));
    flutterEngine.getPlugins().add(new io.flutter.plugins.pathprovider.PathProviderPlugin());
    flutterEngine.getPlugins().add(new com.baseflow.permissionhandler.PermissionHandlerPlugin());
    flutterEngine.getPlugins().add(new io.flutter.plugins.sharedpreferences.SharedPreferencesPlugin());
    flutterEngine.getPlugins().add(new com.tekartik.sqflite.SqflitePlugin());
    flutterEngine.getPlugins().add(new io.flutter.plugins.urllauncher.UrlLauncherPlugin());
    flutterEngine.getPlugins().add(new io.flutter.plugins.videoplayer.VideoPlayerPlugin());
    flutterEngine.getPlugins().add(new creativecreatorormaybenot.wakelock.WakelockPlugin());
  }
}
MainActivity.java
package com.example.youdianle_app_flutter

import android.os.Bundle
import androidx.annotation.NonNull
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.embedding.engine.plugins.shim.ShimPluginRegistry
import io.flutter.plugins.GeneratedPluginRegistrant;
import io.flutter.plugins.MyFlutterPlugin
import io.flutter.plugins.CosFlutterPlugin

// import android.os.Bundle
// import io.flutter.app.FlutterActivity
// import io.flutter.plugins.GeneratedPluginRegistrant
// import io.flutter.plugins.MyFlutterPlugin

class MainActivity: FlutterActivity() {
    //老方法
//    override fun onCreate(savedInstanceState: Bundle?) {
//        super.onCreate(savedInstanceState)
//        GeneratedPluginRegistrant.registerWith(this)
//        MyFlutterPlugin.registerWith(this.registrarFor("com.example.flutter_app/plugin"))
//     //    CosFlutterPlugin.registerWith(this.registrarFor("tencent_cos"))
//    }

    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)

        flutterEngine.plugins.add(MyFlutterPlugin());
        flutterEngine.getPlugins().add(CosFlutterPlugin());

        // GeneratedPluginRegistrant.registerWith(flutterEngine)
        // val shimPluginRegistry = ShimPluginRegistry(flutterEngine)
        // MyFlutterPlugin.registerWith(shimPluginRegistry.registrarFor("com.example.flutter_app/plugin"));

    }
}

flutter调用的代码

//上传图片云
import 'package:flutter/services.dart';
 class UploadPic {
  static const cosPlugin = const MethodChannel('tencent_cos');
  static Future<String> getCos(String path,String way) async {
    Map<String, Object> map = {"localPath": path, "way": way};
    print(path);
    var res = await cosPlugin.invokeMethod("TencentCos.uploadFile", map);
    print(res);
    return res;
  }

}

MethodChannel里的名字要与andriod的一一对应 具体的混合开发基础可以自行学习一下

参考的官方文档

腾讯官方文档 https://cloud.tencent.com/document/product/436/12159
暂时使用的是永久秘钥
后续会考虑改用临时秘钥(官方也推荐使用临时秘钥,永远密码只作为开发环境使用)。

你可能感兴趣的:(flutter,android,java,移动开发,app,安卓)