Skip to content

Commit 8c526d0

Browse files
committed
增加android刷新图库处理
1 parent 26465fc commit 8c526d0

File tree

4 files changed

+54
-4
lines changed

4 files changed

+54
-4
lines changed

android/app/src/main/kotlin/com/shuyu/gsygithub/gsygithubappflutter/MainActivity.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ class MainActivity(): FlutterActivity() {
99
override fun onCreate(savedInstanceState: Bundle?) {
1010
super.onCreate(savedInstanceState)
1111
GeneratedPluginRegistrant.registerWith(this)
12+
UpdateAlbumPlugin.register(this, flutterView)
1213
}
1314
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import android.content.Context
2+
import android.content.Intent
3+
import android.net.Uri
4+
import android.provider.MediaStore
5+
import io.flutter.plugin.common.BinaryMessenger
6+
import io.flutter.plugin.common.MethodChannel
7+
8+
object UpdateAlbumPlugin {
9+
10+
/** Channel名称 **/
11+
private const val ChannelName = "com.shuyu.gsygithub.gsygithubflutter/UpdateAlbumPlugin"
12+
13+
/**
14+
* 注册Toast插件
15+
* @param context 上下文对象
16+
* @param messenger 数据信息交流对象
17+
*/
18+
@JvmStatic
19+
fun register(context: Context, messenger: BinaryMessenger) = MethodChannel(messenger, ChannelName).setMethodCallHandler { methodCall, result ->
20+
when (methodCall.method) {
21+
"updateAlbum" -> {
22+
val path: String = methodCall.argument("path")
23+
val name: String = methodCall.argument("name")
24+
try {
25+
MediaStore.Images.Media.insertImage(context.contentResolver, path, name, null)
26+
} catch (e: Exception) {
27+
e.printStackTrace()
28+
}
29+
// 最后通知图库更新
30+
context.sendBroadcast(Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://$path")))
31+
}
32+
}
33+
result.success(null) //没有返回值,所以直接返回为null
34+
}
35+
36+
}

lib/common/utils/CommonUtils.dart

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,23 +97,29 @@ class CommonUtils {
9797
return appPath;
9898
}
9999

100-
static saveImage(String url) async {
100+
static saveImage(String url) async {
101101
Future<String> _findPath(String imageUrl) async {
102102
final cache = await CacheManager.getInstance();
103103
final file = await cache.getFile(imageUrl);
104-
if(file == null) {
104+
if (file == null) {
105105
return null;
106106
}
107107
Directory localPath = await CommonUtils.getLocalPath();
108-
if(localPath == null) {
108+
if (localPath == null) {
109109
return null;
110110
}
111-
final result = await file.copy(localPath.path + file.path.substring(file.path.lastIndexOf("/")));
111+
final name = splitFileNameByPath(file.path);
112+
final result = await file.copy(localPath.path + name);
112113
return result.path;
113114
}
115+
114116
return _findPath(url);
115117
}
116118

119+
static splitFileNameByPath(String path) {
120+
return path.substring(path.lastIndexOf("/"));
121+
}
122+
117123
static getFullName(String repository_url) {
118124
if (repository_url != null && repository_url.substring(repository_url.length - 1) == "/") {
119125
repository_url = repository_url.substring(0, repository_url.length - 1);

lib/page/PhotoViewPage.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import 'dart:io';
2+
13
import 'package:flutter/material.dart';
4+
import 'package:flutter/services.dart';
25
import 'package:flutter_spinkit/flutter_spinkit.dart';
36
import 'package:fluttertoast/fluttertoast.dart';
47
import 'package:gsy_github_app_flutter/common/style/GSYStyle.dart';
@@ -29,6 +32,10 @@ class PhotoViewPage extends StatelessWidget {
2932
CommonUtils.saveImage(url).then((res) {
3033
if (res != null) {
3134
Fluttertoast.showToast(msg: res);
35+
if (Platform.isAndroid) {
36+
const updateAlbum = const MethodChannel('com.shuyu.gsygithub.gsygithubflutter/UpdateAlbumPlugin');
37+
updateAlbum.invokeMethod('updateAlbum', { 'path': res, 'name': CommonUtils.splitFileNameByPath(res)});
38+
}
3239
}
3340
});
3441
},

0 commit comments

Comments
 (0)