1. 기본 설정
andoid/app/src/main/AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!-- 쓰기권한부여 --> .... </manifest>
AndroidManifest.xml에 쓰기 권한을 추가한다. 권한은 manifest 바로 밑에 추가한다.
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252F404a9fb6-ab9b-45cc-8074-ee63a3334890%252Fe99912ea-c16e-4503-b7c7-4aaebf5e87de%252FUntitled.png%3Ftable%3Dblock%26id%3D2b8847a7-7987-4b5d-ac6e-c08b312fcdb3%26cache%3Dv2&w=3840&q=75)
pubspec.yml에 image_picker 와 gallery_saver 을 추가한다.
android/app/src/build.gradle
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252F404a9fb6-ab9b-45cc-8074-ee63a3334890%252Fcaed0038-b6a6-4a16-9907-a238c6327117%252FUntitled.png%3Ftable%3Dblock%26id%3D32b8283a-f6b0-434c-a577-3454008bfd49%26cache%3Dv2&w=3840&q=75)
build.gradle 에서 해당 부분을 수정한다.
2. 사진 촬영, 저장 어플 만들기
import 'package:flutter/material.dart'; import 'package:gallery_saver/gallery_saver.dart'; import 'package:image_picker/image_picker.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatefulWidget { const MyApp({super.key}); @override State<MyApp> createState() => _MyAppState(); } class _MyAppState extends State<MyApp> { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: Column( children: [ Expanded( child: Center( child: Text( "사진 저장하기", style: TextStyle(fontSize: 50.0), ), ), ), Row( children: [ IconButton( onPressed: () { _takePhoto(); }, icon: Icon(Icons.camera_alt_outlined), iconSize: 50.0, ), ], ), ], ), ), ); } void _takePhoto() async { ImagePicker().pickImage(source: ImageSource.camera).then((value) { if (value != null && value.path != null) { print("저장경로 : ${value.path}"); GallerySaver.saveImage(value.path).then((value) { print("사진이 저장되었습니다."); },); } },); } }
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252F404a9fb6-ab9b-45cc-8074-ee63a3334890%252Fa80feb5d-b4d4-4850-8f7d-5a0466e72907%252FUntitled.png%3Ftable%3Dblock%26id%3Db5b414d5-38a2-4865-8217-e9e8efc72019%26cache%3Dv2&w=3840&q=75)
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252F404a9fb6-ab9b-45cc-8074-ee63a3334890%252F24d80d79-cdda-4cc6-badf-7af5e30de72d%252FUntitled.png%3Ftable%3Dblock%26id%3Da0407add-17a1-4389-9372-aea31a1c4cf8%26cache%3Dv2&w=3840&q=75)
하단의 카메라 버튼을 누르면 가상의 카메라가 뜨면서 사진을 찍을 수 있다.
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252F404a9fb6-ab9b-45cc-8074-ee63a3334890%252F33e464ea-2b00-4e28-9b92-4df70026cf5f%252FUntitled.png%3Ftable%3Dblock%26id%3D0c2a6aea-8e8d-494a-860b-ec806b27873b%26cache%3Dv2&w=3840&q=75)
포토를 들어가면 찍은 사진을 확인할 수 있다.
3. 사진 촬영 저장 및 불러오기 어플 만들기
andoid/app/src/main/AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!-- 쓰기권한부여 --> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <!-- 읽기권한부여 --> .... </manifest>
쓰기 권한과 함께 읽기 권한도 작성한다.
main.dart
import 'dart:io'; import 'package:flutter/material.dart'; import 'package:gallery_saver/gallery_saver.dart'; import 'package:image_picker/image_picker.dart'; void main() { runApp(MyApp()); } class MyApp extends StatefulWidget { @override State<MyApp> createState() => _MyAppState(); } class _MyAppState extends State<MyApp> { File? _selectedImage; @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: Column( children: [ Expanded( child: Center( child: Text( "사진 저장하기", style: TextStyle(fontSize: 50.0), ), ), ), Container( child: _selectedImage != null ? Image.file(_selectedImage!) : Text("사진 없음"), ), Row( mainAxisAlignment: MainAxisAlignment.end, children: [ IconButton( onPressed: _takePhoto, icon: Icon(Icons.camera_alt_outlined), iconSize: 50.0, ), IconButton( onPressed: _pickImageFromGallery, icon: Icon(Icons.image_outlined), iconSize: 50.0, ) ], ), ], ), ), ); } void _takePhoto() async { final pickedImage = await ImagePicker().pickImage(source: ImageSource.camera); if (pickedImage != null) { setState(() { _selectedImage = File(pickedImage.path); }); GallerySaver.saveImage(pickedImage.path).then( (bool? success) { print(success == true ? "사진이 저장되었습니다." : "사진 저장 오류"); }, ); } } void _pickImageFromGallery() async { final pickedImage = await ImagePicker().pickImage(source: ImageSource.gallery); if (pickedImage != null) { setState(() { _selectedImage = File(pickedImage.path); }); } } }
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252F404a9fb6-ab9b-45cc-8074-ee63a3334890%252F3cf2ae0d-0658-4156-ad38-a8e6637c08c8%252FUntitled.png%3Ftable%3Dblock%26id%3D0a42af14-3900-4962-8ff9-27210425469b%26cache%3Dv2&w=3840&q=75)
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252F404a9fb6-ab9b-45cc-8074-ee63a3334890%252F3d37231c-6bd8-4f73-8546-2ca05904b1d5%252FScreenshot_10.png%3Ftable%3Dblock%26id%3Dca8863ce-c72b-4b70-bf9c-33ab65878205%26cache%3Dv2&w=3840&q=75)
카메라 버튼을 누르면 사진도 찍을 수 있고, 오른쪽으 사진 아이콘을 누르면 찍은 사진이 나온다.
Share article