001.
public
class
TakePhoto <export_main_activity>
extends
Activity
implements
View.OnClickListener{
002.
protected
Button btnTakePhoto, btnUpload;
003.
protected
ImageView imageview;
004.
protected
String path_pic =
null
;
005.
protected
boolean
taken;
006.
protected
static
final
String PHOTO_TAKEN =
"photo_taken"
;
007.
String up_name;
008.
Bitmap bitmap =
null
;
009.
TextView txtPhotoName;
010.
011.
012.
@Override
013.
public
void
onCreate(Bundle savedInstanceState) {
014.
super
.onCreate(savedInstanceState);
015.
setContentView(R.layout.activity_takephoto);
016.
017.
txtPhotoName = (TextView)findViewById(R.id.txtPhotoName);
018.
imageview = ( ImageView ) findViewById( R.id.imagePOI );
019.
btnTakePhoto = ( Button ) findViewById( R.id.btnTakePhoto );
020.
btnTakePhoto.setOnClickListener(
this
);
021.
btnUpload = ( Button ) findViewById( R.id.btnUpload);
022.
btnUpload.setOnClickListener(
this
);
023.
024.
025.
File file =
new
File(Environment.getExternalStorageDirectory() +
"/myPhoto/"
);
026.
if
(!file.exists()) {
027.
try
{
028.
file.mkdirs();
029.
}
catch
(Exception e){
030.
e.printStackTrace();
031.
}
032.
}
033.
}
034.
035.
@Override
036.
public
void
onClick(View v) {
037.
if
(v.getId()==R.id.btnTakePhoto){
038.
039.
startCameraActivity();
040.
}
041.
if
(v.getId()==R.id.btnUpload){
042.
if
(SaveData())
043.
{
044.
045.
}
046.
}
047.
}
048.
049.
protected
void
startCameraActivity(){
050.
SimpleDateFormat sdf =
new
SimpleDateFormat(
"yyyyMMddHHmmss"
);
051.
String picTime = sdf.format(
new
Date(
100
));
052.
path_pic = Environment.getExternalStorageDirectory() +
"/TravelThailandPhoto/"
+picTime+
".jpg"
;
053.
up_name = picTime+
".jpg"
;
054.
File file =
new
File(path_pic);
055.
Uri outputFileUri = Uri.fromFile( file );
056.
Intent intent =
new
Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
057.
intent.putExtra( MediaStore.EXTRA_OUTPUT, outputFileUri );
058.
startActivityForResult( intent,
0
);
059.
}
060.
061.
062.
@Override
063.
protected
void
onActivityResult(
int
requestCode,
int
resultCode, Intent data) {
064.
065.
switch
( resultCode )
066.
{
067.
case
0
:
068.
Log.i(
"Tag"
,
"User cancelled"
);
069.
break
;
070.
071.
case
-
1
:
072.
onPhotoTaken();
073.
break
;
074.
}
075.
}
076.
protected
void
onPhotoTaken(){
077.
078.
taken =
true
;
079.
bitmap = decodeFile(path_pic);
080.
imageview.setImageBitmap(bitmap);
081.
}
082.
083.
@Override
084.
protected
void
onSaveInstanceState( Bundle outState ) {
085.
outState.putBoolean( TakePhoto.PHOTO_TAKEN, taken );
086.
}
087.
@Override
088.
protected
void
onRestoreInstanceState( Bundle savedInstanceState){
089.
Log.i(
"Tag"
,
"onRestoreInstanceState()"
);
090.
if
( savedInstanceState.getBoolean( TakePhoto.PHOTO_TAKEN ) ) {
091.
onPhotoTaken();
092.
}
093.
}
094.
095.
public
Bitmap decodeFile(String filePath) {
096.
097.
BitmapFactory.Options o =
new
BitmapFactory.Options();
098.
o.inJustDecodeBounds =
true
;
099.
BitmapFactory.decodeFile(filePath, o);
100.
101.
102.
final
int
REQUIRED_SIZE =
1024
;
103.
104.
105.
int
width_tmp = o.outWidth, height_tmp = o.outHeight;
106.
int
scale =
1
;
107.
while
(
true
) {
108.
if
(width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
109.
break
;
110.
width_tmp /=
2
;
111.
height_tmp /=
2
;
112.
scale *=
2
;
113.
}
114.
115.
116.
BitmapFactory.Options o2 =
new
BitmapFactory.Options();
117.
o2.inSampleSize = scale;
118.
Bitmap bmp = BitmapFactory.decodeFile(filePath, o2);
119.
return
bmp;
120.
121.
122.
}
123.
124.
public
boolean
SaveData()
125.
{
126.
127.
128.
final
EditText txtPhotoName = (EditText) findViewById(R.id.txtPhotoName);
129.
final
EditText txtPhotoDetail = (EditText) findViewById(R.id.txtPhotoDetail);
130.
132.
133.
List<NameValuePair> params =
new
ArrayList<NameValuePair>();
134.
params.add(
new
BasicNameValuePair(
"tPhotoName"
, txtPhotoName.getText().toString()));
135.
params.add(
new
BasicNameValuePair(
"tPhotoDetail"
, txtPhotoDetail.getText().toString()));
136.
137.
138.
139.
140.
141.
String resultServer = Helper.getHttpPost(url,params);
142.
143.
144.
String strStatusID =
"0"
;
145.
String strError =
"Unknow Status!"
;
146.
147.
JSONObject c;
148.
try
{
149.
c =
new
JSONObject(resultServer);
150.
strStatusID = c.getString(
"StatusID"
);
151.
strError = c.getString(
"Error"
);
152.
}
catch
(JSONException e) {
153.
154.
e.printStackTrace();
155.
}
156.
157.
158.
if
(strStatusID.equals(
"0"
))
159.
{
160.
ad.setMessage(strError);
161.
ad.show();
162.
}
163.
else
164.
{
165.
Toast.makeText(TakePhoto.
this
,
"บันทึก Photo เรียบร้อยแล้ว."
, Toast.LENGTH_SHORT).show();
166.
txtPhotoName.setText(
""
);
167.
168.
}
169.
170.
171.
return
true
;
172.
}
173.
174.
public
boolean
onCreateOptionsMenu(Menu menu) {
175.
getMenuInflater().inflate(R.menu.main, menu);
176.
return
true
;
177.
}
178.
}