이번에 알아볼 내용은 앱을 통해서 사진을 업로는 하는 것이다.
필자는 앱으로 WCF를 호출하여 서버의 특정폴더에 이미지를 저장하고 이미지 파일명을 DB에 저장하는 방식을 썻다.
이를 간단한 구조도로 표현하면 다음과 같다.
그러면 Android의 코드를 살펴보도록 하겠다.
Bitmap photo = BitmapFactory.decodeResource(getResources(), R.drawable.screenshot);
//This code is to convert Image to String.
ByteArrayOutputStream bao = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 90, bao);
byte [] ba = bao.toByteArray();
String ba1 = Base64.encodeToString(ba, Base64.DEFAULT);
이렇게 해서 Image를 Base64의 String 형태로 변환하였다.
이를 http POST,GET 방식으로 전송을 하면된다.
WCF에서 이미지를 서버의 특정폴더에 저장하는 C# 코드는 다음과 같다.
byte[] arr = System.Convert.FromBase64String(photointerface.Image);
Encoding encode;
encode = System.Text.Encoding.Default;
FileStream FileStr = new FileStream("c:\\"+photointerface.Name, FileMode.OpenOrCreate, FileAccess.Write);
// 버퍼의 데이터를 사용하여 이 스트림에 바이트 블록을 씀
FileStr.Write(arr, 0, arr.Length);
// Save File
BinaryWriter Savefile = new BinaryWriter(FileStr, encode);
Savefile.Close();
'Programming > Android' 카테고리의 다른 글
[Android] 해상도에 따른 비트맵 리사이즈 (0) | 2014.04.18 |
---|---|
[안드로이드 Android] 어플리케이션 로딩화면(Splash) 구현하기 (0) | 2013.06.27 |
[안드로이드 Android] Tab (TabHost) (0) | 2013.06.27 |
[안드로이드 Android] f3 보이게 하는 방법(android.jar source 보이는 방법) (0) | 2013.06.27 |