[英]flutter web : When I image upload to storage, I can't control the quality of the image picker
我使用的sdk如下。
sdk: ">=2.7.0 <3.0.0"
image_picker: ^0.6.7
image_picker_for_web: ^0.1.0
firebase_storage: ^3.1.6
cloud_firestore: ^0.13.7
將圖像上傳到存儲時,我嘗試減小圖像文件的大小。 這是圖片上傳的代碼。
import 'package:image_picker/image_picker.dart';
import 'package:firebase/firebase.dart' as fb;
import 'package:omd_test/widget/custom_loading.dart';
class TestScreen extends StatefulWidget {
@override
State<TestScreen> createState() => _TestScreenState();
}
class _TestScreenState extends State<TestScreen> {
PickedFile _image;
String imageUrl;
@override
Widget build(BuildContext context) {
return SafeArea(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Scaffold(
appBar: AppBar(
title: Text("TEST"),
),
body: Padding(
padding: const EdgeInsets.all(12.0),
child: Column(
children: [
Container(
width: 200,
height: 200,
child: _image == null
? Container()
: Image.network(
_image.path,
)),
TextButton(
onPressed: () {
_getImagesPicker();
},
child: Text(
"Pick",
style: TextStyle(color: Colors.black),
)),
SizedBox(
height: 20,
),
TextButton(
onPressed: () async {
if (_image != null) {
await imageUpload(_image);
setState(() {
_image = null;
});
} else
print("No Image");
},
child: Text(
"Upload"
)),
],
),
),
),
),
);
}
Future<void> _getImagesPicker() async {
try {
final _imagePath = await ImagePicker().getImage(
source: ImageSource.gallery,
imageQuality: 10,
maxHeight: 50,
maxWidth: 50
);
setState(() {
_image = _imagePath;
});
} catch (err) {
print(err);
}
}
Future<String> imageUpload(PickedFile image) async {
var bytes = await image.readAsBytes();
fb.StorageReference _storage =
fb.storage().ref('testScreen/${image.path}.jpg');
fb.UploadTaskSnapshot uploadTaskSnapshot = await _storage
.put(bytes, fb.UploadMetadata(contentType: 'image/png'))
.future;
var imageUri = await uploadTaskSnapshot.ref.getDownloadURL();
imageUrl = imageUri.toString();
return imageUrl;
}
}
我通過更改 ImagePicker 的“imageQuality”值進行了測試,但是當上傳到存儲時,所有圖像文件的大小都相同。
我能知道問題是什么嗎? 上傳到存儲有問題嗎?
謝謝你。
從image_picker
存儲庫中,可以對什么類型的圖像進行哪些調整存在一些不一致的地方。
這是一個概述,可能會提供一些重要的線索。
我能知道問題是什么嗎?
package 的實際實現中的注釋有幾行[這里] :
//Note that the `maxWidth`, `maxHeight` and `imageQuality` arguments are not supported on the web. If any of these arguments is supplied, it'll be silently ignored by the web version of the plugin.
但是,仍然存在調整所選圖像大小的實現。
上傳到存儲有問題嗎?
貌似沒有。
以下是您可能希望繼續嘗試的內容; 在其他平台上運行您的應用程序並嘗試確定圖像是否已調整大小,這將幫助您找出是否僅在 web 上沒有調整質量。
如果您確定是這樣,您可能希望自己實現所選媒體的質量/大小調整,您可以利用kIsWeb
boolean 屬性來確定它是否是 web,然后應用您自己的實現。
if(kIsWeb){
//Adjust quality/size
}else{
//use the already adjusted media
}
//Compression is only supported for certain image types such as JPEG and on Android PNG and WebP, too. If compression is not supported for the image that is picked, a warning message will be logged.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.