diff --git a/example/lib/main.dart b/example/lib/main.dart index b1f0bf3..bd7823e 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -1,8 +1,12 @@ import 'dart:async'; import 'dart:io'; +import 'dart:typed_data'; import 'package:flutter/material.dart'; import 'package:flutter_cache_manager/flutter_cache_manager.dart'; +import 'package:image/image.dart' as imglib; +import 'package:path/path.dart' as path; +import 'package:path_provider/path_provider.dart' as path_provider; import 'package:weibo_kit/weibo_kit.dart'; import 'package:weibo_kit_example/api/model/weibo_api_resp.dart'; import 'package:weibo_kit_example/api/weibo_api.dart'; @@ -34,6 +38,7 @@ class Home extends StatefulWidget { const Home({ super.key, }); + @override State createState() { return _HomeState(); @@ -90,8 +95,7 @@ class _HomeState extends State { ListTile( title: Text('环境检查'), onTap: () async { - final String content = - 'weibo: ${await Weibo.instance.isInstalled()}'; + final String content = 'weibo: ${await Weibo.instance.isInstalled()}'; _showTips('环境检查', content); }, ), @@ -108,18 +112,15 @@ class _HomeState extends State { title: Text('用户信息'), onTap: () async { if (_authResp?.isSuccessful ?? false) { - final WeiboUserInfoResp userInfoResp = - await WeiboApi.getUserInfo( + final WeiboUserInfoResp userInfoResp = await WeiboApi.getUserInfo( appkey: _WEIBO_APP_KEY, userId: _authResp!.userId!, accessToken: _authResp!.accessToken!, ); if (userInfoResp.isSuccessful) { - _showTips('用户信息', - '${userInfoResp.screenName}\n${userInfoResp.description}\n${userInfoResp.location}\n${userInfoResp.profileImageUrl}'); + _showTips('用户信息', '${userInfoResp.screenName}\n${userInfoResp.description}\n${userInfoResp.location}\n${userInfoResp.profileImageUrl}'); } else { - _showTips('用户信息', - '获取用户信息失败\n${userInfoResp.errorCode}:${userInfoResp.error}'); + _showTips('用户信息', '获取用户信息失败\n${userInfoResp.errorCode}:${userInfoResp.error}'); } } }, @@ -135,32 +136,43 @@ class _HomeState extends State { ListTile( title: Text('图片分享'), onTap: () async { - final File file = await DefaultCacheManager().getSingleFile( - 'https://www.baidu.com/img/bd_logo1.png?where=super'); - await Weibo.instance.shareImage( + File file = await DefaultCacheManager().getSingleFile('https://www.baidu.com/img/bd_logo1.png?where=super'); + if (Platform.isAndroid) { + // 仅支持 Context.getExternalFilesDir(null)/Context.getExternalCacheDirs(null) 路径分享 + // path_provider.getExternalCacheDirectories(); + // path_provider.getExternalStorageDirectory(); + final Directory temporaryDir = await path_provider.getTemporaryDirectory(); + if (path.isWithin(temporaryDir.parent.path, file.path)) { + // 复制 + final File copyFile = File(path.join((await path_provider.getExternalStorageDirectory())!.path, path.basename(file.path))); + if (copyFile.existsSync()) { + await copyFile.delete(); + } + await copyFile.writeAsBytes(await file.readAsBytes()); + file = copyFile; + } + } + await Weibo.instance.shareMultiImage( text: 'Share Text', - imageUri: Uri.file(file.path), + imageUris: [Uri.file(file.path)], ); }, ), ListTile( title: Text('网页分享'), onTap: () async { - // final File file = await DefaultCacheManager().getSingleFile( - // 'https://www.baidu.com/img/bd_logo1.png?where=super'); - // final image.Image thumbnail = - // image.decodeImage(file.readAsBytesSync())!; - // Uint8List thumbData = thumbnail.getBytes(); - // if (thumbData.length > 32 * 1024) { - // thumbData = Uint8List.fromList(image.encodeJpg(thumbnail, - // quality: 100 * 32 * 1024 ~/ thumbData.length)); - // } - // await Weibo.instance.shareWebpage( - // title: 'title', - // description: 'share webpage', - // thumbData: thumbData.buffer.asUint8List(), - // webpageUrl: 'https://www.baidu.com', - // ); + final File file = await DefaultCacheManager().getSingleFile('https://www.baidu.com/img/bd_logo1.png?where=super'); + final imglib.Image thumbnail = imglib.decodeImage(file.readAsBytesSync())!; + Uint8List thumbData = thumbnail.getBytes(); + if (thumbData.length > 32 * 1024) { + thumbData = Uint8List.fromList(imglib.encodeJpg(thumbnail, quality: 100 * 32 * 1024 ~/ thumbData.length)); + } + await Weibo.instance.shareWebpage( + title: 'title', + description: 'share webpage', + thumbData: thumbData.buffer.asUint8List(), + webpageUrl: 'https://www.baidu.com', + ); }, ), ],