mirror of
https://github.com/twonlyapp/twonly-app.git
synced 2026-01-15 20:58:41 +00:00
fix #255
This commit is contained in:
parent
aa4654344b
commit
636d4de1b6
3 changed files with 18 additions and 35 deletions
|
|
@ -1,40 +1,10 @@
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
import 'dart:ui';
|
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
|
||||||
import 'package:flutter_image_compress/flutter_image_compress.dart';
|
import 'package:flutter_image_compress/flutter_image_compress.dart';
|
||||||
import 'package:path/path.dart';
|
import 'package:path/path.dart';
|
||||||
import 'package:path_provider/path_provider.dart';
|
|
||||||
import 'package:twonly/src/utils/log.dart';
|
import 'package:twonly/src/utils/log.dart';
|
||||||
import 'package:video_thumbnail/video_thumbnail.dart';
|
import 'package:video_thumbnail/video_thumbnail.dart';
|
||||||
|
|
||||||
Future<void> createThumbnails(String directoryPath) async {
|
|
||||||
final directory = Directory(directoryPath);
|
|
||||||
final outputDirectory = await getTemporaryDirectory();
|
|
||||||
|
|
||||||
if (directory.existsSync()) {
|
|
||||||
final files = directory.listSync();
|
|
||||||
|
|
||||||
for (final file in files) {
|
|
||||||
if (file is File) {
|
|
||||||
final filePath = file.path;
|
|
||||||
final fileExtension = filePath.split('.').last.toLowerCase();
|
|
||||||
|
|
||||||
if (['jpg', 'jpeg', 'png'].contains(fileExtension)) {
|
|
||||||
// Create thumbnail for images
|
|
||||||
final image = await decodeImageFromList(file.readAsBytesSync());
|
|
||||||
final thumbnail = await image.toByteData(format: ImageByteFormat.png);
|
|
||||||
final thumbnailFile =
|
|
||||||
File('${outputDirectory.path}/${file.uri.pathSegments.last}');
|
|
||||||
await thumbnailFile.writeAsBytes(thumbnail!.buffer.asUint8List());
|
|
||||||
} else if (['mp4', 'mov', 'avi'].contains(fileExtension)) {
|
|
||||||
// Create thumbnail for videos
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> createThumbnailsForImage(File file) async {
|
Future<void> createThumbnailsForImage(File file) async {
|
||||||
final fileExtension = file.path.split('.').last.toLowerCase();
|
final fileExtension = file.path.split('.').last.toLowerCase();
|
||||||
if (fileExtension != 'png') {
|
if (fileExtension != 'png') {
|
||||||
|
|
@ -47,8 +17,8 @@ Future<void> createThumbnailsForImage(File file) async {
|
||||||
minHeight: 800,
|
minHeight: 800,
|
||||||
minWidth: 450,
|
minWidth: 450,
|
||||||
file.path,
|
file.path,
|
||||||
format: CompressFormat.png,
|
format: CompressFormat.webp,
|
||||||
quality: 30,
|
quality: 50,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (imageBytesCompressed == null) {
|
if (imageBytesCompressed == null) {
|
||||||
|
|
|
||||||
|
|
@ -26,12 +26,18 @@ extension ShortCutsExtension on BuildContext {
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<String?> saveImageToGallery(Uint8List imageBytes) async {
|
Future<String?> saveImageToGallery(Uint8List imageBytes) async {
|
||||||
|
final jpgImages = await FlutterImageCompress.compressWithList(
|
||||||
|
// ignore: avoid_redundant_argument_values
|
||||||
|
format: CompressFormat.jpeg,
|
||||||
|
imageBytes,
|
||||||
|
quality: 100,
|
||||||
|
);
|
||||||
final hasAccess = await Gal.hasAccess();
|
final hasAccess = await Gal.hasAccess();
|
||||||
if (!hasAccess) {
|
if (!hasAccess) {
|
||||||
await Gal.requestAccess();
|
await Gal.requestAccess();
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
await Gal.putImageBytes(imageBytes);
|
await Gal.putImageBytes(jpgImages);
|
||||||
return null;
|
return null;
|
||||||
} on GalException catch (e) {
|
} on GalException catch (e) {
|
||||||
return e.type.message;
|
return e.type.message;
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import 'dart:math';
|
||||||
import 'dart:typed_data';
|
import 'dart:typed_data';
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_image_compress/flutter_image_compress.dart';
|
||||||
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
|
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
|
||||||
import 'package:path/path.dart';
|
import 'package:path/path.dart';
|
||||||
import 'package:twonly/src/services/api/media_upload.dart';
|
import 'package:twonly/src/services/api/media_upload.dart';
|
||||||
|
|
@ -75,10 +76,16 @@ class SaveToGalleryButtonState extends State<SaveToGalleryButton> {
|
||||||
res = await saveVideoToGallery(widget.videoFilePath!.path);
|
res = await saveVideoToGallery(widget.videoFilePath!.path);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
memoryPath += '.png';
|
|
||||||
final imageBytes = await widget.getMergedImage();
|
final imageBytes = await widget.getMergedImage();
|
||||||
if (imageBytes == null || !mounted) return;
|
if (imageBytes == null || !mounted) return;
|
||||||
await File(memoryPath).writeAsBytes(imageBytes);
|
final webPImageBytes =
|
||||||
|
await FlutterImageCompress.compressWithList(
|
||||||
|
format: CompressFormat.webp,
|
||||||
|
imageBytes,
|
||||||
|
quality: 100,
|
||||||
|
);
|
||||||
|
memoryPath += '.png';
|
||||||
|
await File(memoryPath).writeAsBytes(webPImageBytes);
|
||||||
unawaited(createThumbnailsForImage(File(memoryPath)));
|
unawaited(createThumbnailsForImage(File(memoryPath)));
|
||||||
if (storeToGallery) {
|
if (storeToGallery) {
|
||||||
res = await saveImageToGallery(imageBytes);
|
res = await saveImageToGallery(imageBytes);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue