Skip to content

Commit

Permalink
upload_imageDone
Browse files Browse the repository at this point in the history
  • Loading branch information
msabhinavchandra committed Apr 25, 2024
1 parent 0c3eca6 commit 0febe13
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Flutter/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.hardware.CAMERA" />
<uses-permission android:name="android.hardware.camera" />
<uses-permission android:name="android.hardware.camera.autofocus" />

<application
android:label="flutterbasics"
android:name="${applicationName}"
Expand Down
5 changes: 3 additions & 2 deletions Flutter/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import 'package:flutter/material.dart';
import 'package:flutterbasics/ChatScreen.dart';
import 'package:flutterbasics/DashBoardScreen.dart';
import 'package:flutterbasics/Speech_To_Text.dart';
import 'camera.dart';

import 'upload_image.dart';

void main() {
runApp(MyApp());
Expand Down Expand Up @@ -55,7 +56,7 @@ class HomePage extends StatelessWidget {
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const MyApp2()),
MaterialPageRoute(builder: (context) => const UploadImageScreen()),
);
},
child: const Icon(Icons.camera_alt),
Expand Down
181 changes: 181 additions & 0 deletions Flutter/lib/upload_image.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
import 'dart:io';
// import 'dart:js_interop';
import 'package:flutter/widgets.dart';
import 'package:http/http.dart' as http;
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:modal_progress_hud_nsn/modal_progress_hud_nsn.dart';

class UploadImageScreen extends StatefulWidget {
const UploadImageScreen({Key? key}) : super(key: key);

@override
_UploadImageScreenState createState() => _UploadImageScreenState();
}

class _UploadImageScreenState extends State<UploadImageScreen> {
File? imageGal, imageCam;
final _pickerGal = ImagePicker();
final _pickerCam = ImagePicker();
bool showSpinner = false;

Future getImageGL() async {
final pickedFile_Gallery = await _pickerGal.pickImage(
source: ImageSource.gallery, imageQuality: 80);

if (pickedFile_Gallery != null) {
imageGal = File(pickedFile_Gallery.path);
setState(() {});
uploadImageFromGallery();
} else {
print("No image selected");
}
}

Future getImageCM() async {
final pickedFile_Camera = await _pickerCam.pickImage(
source: ImageSource.camera, imageQuality: 80);

if (pickedFile_Camera != null) {
imageCam = File(pickedFile_Camera.path);
setState(() {});
uploadImageFromCamera();
} else {
print("No image Captured");
}
}

Future<void> uploadImageFromGallery() async {
setState(() {
showSpinner = true;
});
var stream = new http.ByteStream(imageGal!.openRead());
stream.cast();

var length = await imageGal!.length();

//coming soon.
var uri = Uri.parse("https://fakestoreapi.com/products");

var request = new http.MultipartRequest('POST', uri);

request.fields['title'] = "Static title";

var multiport = new http.MultipartFile('image', stream, length);

request.files.add(multiport);

var response = await request.send();

if (response.statusCode == 200) {
setState(() {
showSpinner = false;
});

print("image Uploaded");
} else {
print("failed to upload");
setState(() {
showSpinner = true;
});
}
}

Future<void> uploadImageFromCamera() async {
setState(() {
showSpinner = true;
});
var stream = new http.ByteStream(imageCam!.openRead());
stream.cast();

var length = await imageCam!.length();

//coming soon.
var uri = Uri.parse("https://fakestoreapi.com/products");

var request = new http.MultipartRequest('POST', uri);

request.fields['title'] = "Static title";

var multiport = new http.MultipartFile('image', stream, length);

request.files.add(multiport);

var response = await request.send();

if (response.statusCode == 200) {
setState(() {
showSpinner = false;
});

print("image Uploaded");
} else {
print("failed to upload");
setState(() {
showSpinner = true;
});
}
}

@override
Widget build(BuildContext context) {
return ModalProgressHUD(
inAsyncCall: showSpinner,
child: Scaffold(
appBar: AppBar(
title: const Text('Upload Image'),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
GestureDetector(
onTap: () {
getImageGL();
},
child: Container(
child: imageGal == null
? Center(
child: Text("Pick an image"),
)
: Container(
child: Center(
child: Image.file(
File(imageGal!.path).absolute,
height: 100,
width: 100,
fit: BoxFit.cover,
),
),
),
),
),
GestureDetector(
onTap: () {
getImageCM();
},
child: Container(
child: imageCam == null
? Center(
child: Text("Click an image"),
)
: Container(
child: Center(
child: Image.file(
File(imageCam!.path).absolute,
height: 100,
width: 100,
fit: BoxFit.cover,
),
),
),
),
),
SizedBox(
height: 150,
),
],
),
));
}
}

0 comments on commit 0febe13

Please sign in to comment.