Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
ethan
/
appframe
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
Commit 9587dbe0
authored
2026-01-13 17:17:34 +0800
by
tanghuan
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
保存图片/视频到相册的指令,增加对base64编码的图片处理
1 parent
800d6117
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
58 additions
and
12 deletions
lib/data/repositories/message/save_to_album_handler.dart
lib/data/repositories/message/save_to_album_handler.dart
View file @
9587dbe
import
'dart:convert'
;
import
'dart:io'
;
import
'package:appframe/services/dispatcher.dart'
;
import
'package:appframe/services/dispatcher.dart'
;
import
'package:gallery_saver_plus/gallery_saver.dart'
;
import
'package:gallery_saver_plus/gallery_saver.dart'
;
import
'package:path_provider/path_provider.dart'
;
class
SaveToAlbumHandler
extends
MessageHandler
{
class
SaveToAlbumHandler
extends
MessageHandler
{
@override
@override
...
@@ -13,23 +16,21 @@ class SaveToAlbumHandler extends MessageHandler {
...
@@ -13,23 +16,21 @@ class SaveToAlbumHandler extends MessageHandler {
return
false
;
return
false
;
}
}
bool
isVideo
=
String
actualFilePath
;
filePath
.
endsWith
(
'.mp4'
)
||
if
(
_isBase64
(
filePath
))
{
filePath
.
endsWith
(
'.avi'
)
||
actualFilePath
=
await
_saveBase64ToFile
(
filePath
);
filePath
.
endsWith
(
'.mov'
)
||
}
else
{
filePath
.
endsWith
(
'.mkv'
)
||
actualFilePath
=
filePath
;
filePath
.
endsWith
(
'.wmv'
)
||
}
filePath
.
endsWith
(
'.flv'
)
||
filePath
.
endsWith
(
'.webm'
)
||
bool
isVideo
=
_isVideoFile
(
actualFilePath
);
filePath
.
endsWith
(
'.m4v'
)
||
filePath
.
endsWith
(
'.3gp'
);
try
{
try
{
if
(
isVideo
)
{
if
(
isVideo
)
{
final
bool
?
success
=
await
GallerySaver
.
saveVideo
(
f
ilePath
);
final
bool
?
success
=
await
GallerySaver
.
saveVideo
(
actualF
ilePath
);
return
success
??
false
;
return
success
??
false
;
}
else
{
}
else
{
final
bool
?
success
=
await
GallerySaver
.
saveImage
(
f
ilePath
);
final
bool
?
success
=
await
GallerySaver
.
saveImage
(
actualF
ilePath
);
return
success
??
false
;
return
success
??
false
;
}
}
}
catch
(
e
)
{
}
catch
(
e
)
{
...
@@ -37,4 +38,49 @@ class SaveToAlbumHandler extends MessageHandler {
...
@@ -37,4 +38,49 @@ class SaveToAlbumHandler extends MessageHandler {
return
false
;
return
false
;
}
}
}
}
bool
_isBase64
(
String
str
)
{
if
(
str
.
startsWith
(
'data:image/'
))
{
return
true
;
}
try
{
base64Decode
(
str
);
return
true
;
}
catch
(
e
)
{
return
false
;
}
}
Future
<
String
>
_saveBase64ToFile
(
String
base64Str
)
async
{
String
base64Data
=
base64Str
;
String
fileExtension
=
'png'
;
if
(
base64Str
.
startsWith
(
'data:image/'
))
{
final
regExp
=
RegExp
(
r'data:image/(\w+);base64,(.+)'
);
final
match
=
regExp
.
firstMatch
(
base64Str
);
if
(
match
!=
null
)
{
fileExtension
=
match
.
group
(
1
)
??
'png'
;
base64Data
=
match
.
group
(
2
)
??
''
;
}
}
final
bytes
=
base64Decode
(
base64Data
);
final
tempDir
=
await
getTemporaryDirectory
();
final
filePath
=
'
${tempDir.path}
/
${DateTime.now().millisecondsSinceEpoch}
.
$fileExtension
'
;
final
file
=
File
(
filePath
);
await
file
.
writeAsBytes
(
bytes
);
return
filePath
;
}
bool
_isVideoFile
(
String
filePath
)
{
return
filePath
.
endsWith
(
'.mp4'
)
||
filePath
.
endsWith
(
'.avi'
)
||
filePath
.
endsWith
(
'.mov'
)
||
filePath
.
endsWith
(
'.mkv'
)
||
filePath
.
endsWith
(
'.wmv'
)
||
filePath
.
endsWith
(
'.flv'
)
||
filePath
.
endsWith
(
'.webm'
)
||
filePath
.
endsWith
(
'.m4v'
)
||
filePath
.
endsWith
(
'.3gp'
);
}
}
}
Write
Preview
Styling with
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment