我正在尝试通过我的应用程序将图像保存到服务器上的数据库和图片文件夹中。代码有效,但图片不显示。获取损坏的图片如下:
我试图修复它很长时间但无法解决它。我是 Flutter 的新手。
谁能帮帮我。
完整代码如下:
<?php
$photo_url= $_POST['photo_url'];
$NameImage = $_POST['NameImage'];
include 'connt.php';
$path = "upload_pic/$NameImage.png";
$actualpath = "https://**********/PHP/admin/rest/$path";
$sql = "INSERT INTO tbl_photos (photo_url) VALUES (?)";
$stmt = $con->prepare($sql);
$stmt->bind_param("s",$actualpath);
$stmt->execute();
$result = $stmt->get_result();
// if(mysqli_query($conn,$sql)){
file_put_contents($path,base64_decode($photo_url));
echo "Successfully Uploaded";
// }
$stmt->close();
?>
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: '',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: StoreAddScreen(),
);
}
}
class StoreAddScreen extends StatefulWidget {
StoreAddScreen({Key key}) : super(key: key);
@override
_StoreAddScreenState createState() => _StoreAddScreenState();
}
class _StoreAddScreenState extends State<StoreAddScreen> {
List<String> photoPaths = List<String>();
final picker = ImagePicker();
static final String uploadEndPoint =
'https://*************PHP/admin/rest/AddNewCategrytesttodelete.php';
void SaveDate() async {
for (int i = 0; i < photoPaths.length; i++) {
if (photoPaths[i].runtimeType == String) {
String strPath = sprintf("thumb_file_%d", [i]);
print(
'Test_Code: Length ${photoPaths.length} try num: ' + i.toString());
var url = uploadEndPoint;
var response = await http.post(url, body: {
"photo_url": photoPaths[i],
"NameImage": strPath,
});
if (response.statusCode == 200) {
} else {}
}
}
}
static const int MAX_PHOTO_UPLOAD = 5;
@override
Widget build(BuildContext context) {
return Scaffold(
body: SingleChildScrollView(
child: Column(
children: [
SizedBox(
width: double.infinity,
height: 300.0,
child: Expanded(
child: Padding(
padding: EdgeInsets.all(20),
child: SingleChildScrollView(
child: SizedBox(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Padding(
padding: const EdgeInsets.only(top: 10),
child: SizedBox(
height: 140,
child: Row(
children: [
Expanded(
child: Container(
decoration: BoxDecoration(
color: Theme.of(context)
.textTheme
.bodyText1
.color,
borderRadius: BorderRadius.circular(8),
),
child: ListView.builder(
itemCount: photoPaths.length,
scrollDirection: Axis.horizontal,
itemBuilder:
(BuildContext context, int index) {
return InkWell(
child: Padding(
padding: EdgeInsets.only(
top: 8.0,
bottom: 8.0,
left: 8.0,
right: 8.0),
child: Container(
height: 140,
width: 140,
child: ClipRRect(
borderRadius:
BorderRadius.circular(
8),
child: AspectRatio(
aspectRatio: 1.2,
child: Image.file(
File(photoPaths[index]),
fit: BoxFit.cover,
width: 300,
height: 300,
),
),
))),
onTap: () {
showDialog(
context: this.context,
child: new AlertDialog(
content: new FlatButton(
child:
new Text("delete_photo"),
onPressed: () => setState(() {
photoPaths.removeAt(index);
}),
),
),
);
},
);
},
),
),
),
Padding(
padding: const EdgeInsets.only(
left: 10, right: 10),
child: Container(
width: 50,
height:
MediaQuery.of(context).size.height,
decoration: BoxDecoration(
color: Theme.of(context).accentColor,
borderRadius: BorderRadius.circular(8),
),
child: Material(
color: Theme.of(context).accentColor,
borderRadius: BorderRadius.circular(8),
child: InkWell(
borderRadius:
BorderRadius.circular(8),
child: Icon(
Icons.add_a_photo,
color: Theme.of(context)
.floatingActionButtonTheme
.foregroundColor,
),
onTap: () async {
if (photoPaths.length ==
MAX_PHOTO_UPLOAD) {
showDialog(
context: this.context,
child: new AlertDialog(
content: new FlatButton(
child: new Text(
"max_photos_reached"),
),
),
);
}
PickedFile pickedFile =
await picker.getImage(
source:
ImageSource.gallery,
imageQuality: 90);
setState(() {
if (pickedFile != null) {
photoPaths
.add(pickedFile.path);
}
});
}),
)),
)
],
),
),
),
Padding(
padding: const EdgeInsets.only(top: 16),
child: Center(
child: FlatButton(
color: Theme.of(context).accentColor,
// controller: _btnController,
onPressed: SaveDate,
child: Text(
("addd"),
style: TextStyle(
fontWeight: FontWeight.w500,
color: Theme.of(context)
.floatingActionButtonTheme
.foregroundColor,
),
),
),
),
),
SizedBox(
height: 50,
),
],
),
),
),
),
),
),
],
),
));
}
}