【发布时间】:2020-10-29 13:19:54
【问题描述】:
这是我遇到的问题:
我的用户可以从 Facebook(jpeg 或 gif)或本地设备(可以是 png 或 jpg 或其他)设置他们的个人资料图片。
我通过以下方式从 Facebook 获取图片:
// Get the name, email and picture
final graphResponse = await http.get(
'https://graph.facebook.com/v4.0/me?fields=name,email,picture.width(300).height(300)&access_token=$token');
// Decode JSON
final profile = jsonDecode(graphResponse.body);
final String stringData = profile['picture']['data'];
final bytes = Uint8List.fromList(stringData.codeUnits);
并通过以下方式从本地设备获取图像:
final imagePicker = ImagePicker();
// Call image picker
final pickedFile = await imagePicker.getImage(
source: ImageSource.gallery,
maxWidth: MAX_WIDTH_PROFILE_IMAGE,
);
final imageBytes = await pickedFile.readAsBytes();
那么我这里得到的都是字节(Uint8List),如何按照原来的扩展名保存呢?
那么稍后我如何在不检查其扩展名的情况下再次阅读它们?
如:
// Setting the filename
// Could be jpg or png or bmp or gif.
// How to determine the extension?
final filename = 'myProfileImage';
// Getting App's local directory
final Directory localRootDirectory =
await getApplicationDocumentsDirectory();
final String filePath = p.join(localRootDirectory.path, path, filename);
final file = File(filePath);
您看,在读取文件时,我们需要指定完整的文件名。但是如何确定扩展名呢?
【问题讨论】: