image_picker 플러그인 을 사용하려고합니다 . 이 플러그인을 사용하여 이미지를 파일로 가져올 수 있습니다. 이 이미지를 바이트로 변환하고 api로 보내야합니다. 그래서 dart : convert를 사용하여 이미지를 바이트 문자열로 변환하려고했습니다. 이제 디코딩하면 Uint8List 유형 이 생깁니다 . 이것을 파일로 변환하고 Image.file ()에 표시하는 방법 . 여기에서 진행할 수 없습니다. 누군가 나를 도울 수 있습니까?
내가 API 응답에서 얻은이 decodedBytes를 얻고 있다고 생각하십시오. 어떻게 그들을 이미지 위젯에 표시하도록 변환 할 수 있습니까?
지금까지 시도한 코드입니다.
var image = await ImagePicker.pickImage(source: ImageSource.camera);
setState(() {
imageURI = image;
final bytes = image.readAsBytesSync();
String img64 = base64Encode(bytes);
print(bytes);
print(img64);
final decodedBytes = base64Decode(img64);
print(decodedBytes);
//consider i am getting this decodedBytes i am getting from a api response, how can i convert them to display in a Image widget
});
writeAsBytesSync () 사용 하여이 오류가 발생합니다 .
Unhandled Exception: FileSystemException: Cannot open file, path = 'decodedimg.png'
이 오류는 응용 프로그램 샌드 박스의 임의 위치에 쓸 수 없기 때문에 발생합니다. path_provider 를 사용 하여 임시 디렉토리를 찾을 수 있습니다 .
그러나 귀하의 경우에는 image
객체를 사용하고 pickImage
이미 File 객체를 반환하므로Image.file(image)
base64를 임시 디렉토리로 디코딩하려면 다음을 사용할 수 있습니다.
import 'package:path_provider/path_provider.dart';
import 'package:path/path.dart' as path;
Future<File> writeImageTemp(String base64Image, String imageName) async {
final dir = await getTemporaryDirectory();
await dir.create(recursive: true);
final tempFile = File(path.join(dir.path, imageName));
await tempFile.writeAsBytes(base64.decode(base64Image));
return tempFile;
}
pubspec.yaml 사용 :
dependencies:
path: ^1.6.0
path_provider: ^1.6.7
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다