我想从图库中选择图像,但是当我尝试将其保存为共享首选项时。我发现image_picker提供了 tmp / image_picker_4415467867A964-791E-4AFA995BA-18295-0003861F9255294A.jpg
这样的临时位置这不是图像的真实路径。我应该如何获得图像的原始位置路径以供以后使用?
或者我要将整个图像保存在数据库中。怎么办? pickimage()现在已弃用?
请帮助
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Fluter demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
File _image;
String _imageloc;
@override
void initState() {
super.initState();
LoadImage();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Image Picker'),
),
body: Container(
alignment: Alignment.center,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
_imageloc != null
? CircleAvatar(
backgroundImage: FileImage(File(_imageloc)),
radius: 80,
)
: CircleAvatar(
backgroundImage: _image != null
? FileImage(_image)
: NetworkImage(
'https://www.publicdomainpictures.net/pictures/320000/velka/background-image.png'),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: RaisedButton(
onPressed: () {
PickImage();
},
child: Text('Pick Image'),
),
),
RaisedButton(
onPressed: () {
saveImage(_image.path);
},
child: Text('saved'),
),
],
),
),
);
}
void PickImage() async {
var image = await ImagePicker.pickImage(source: ImageSource.gallery);
print(image.path);
setState(() {
_image = image;
});
}
void saveImage(_imageloc) async {
SharedPreferences preferences = await SharedPreferences.getInstance();
setState(() {
preferences.setString('imageloc', _imageloc);
});
}
void LoadImage() async {
SharedPreferences saveimage = await SharedPreferences.getInstance();
setState(() {
saveimage.getString('imageloc');
});
}
}
答案 0 :(得分:2)
好吧,如果以后需要图像,则需要复制并将其保存在某处。实际的图像,而不仅仅是路径。如果我在您的应用中选择图片,则希望将其保存。当我从图库中删除图片或切换手机时,我不希望它消失。
您拥有的路径足以读取图像并将其保存在所需的位置。您的后端极有可能,因为即使是在另一部手机上,我也想拥有自己的照片。
您的操作方式很大程度上取决于您的特定后端,但是我敢肯定会有相应的教程。