001.
package
com.myapp;
002.
003.
import
android.os.Bundle;
004.
import
android.os.Environment;
005.
import
android.provider.MediaStore;
006.
007.
import
java.io.File;
008.
import
java.io.IOException;
009.
import
java.text.SimpleDateFormat;
010.
import
java.util.Date;
011.
012.
import
android.app.Activity;
013.
import
android.content.Intent;
014.
import
android.graphics.Bitmap;
015.
import
android.graphics.BitmapFactory;
016.
import
android.net.Uri;
017.
import
android.view.View;
018.
import
android.view.Menu;
019.
import
android.widget.Button;
020.
import
android.widget.ImageView;
021.
022.
public
class
MainActivity
extends
Activity {
023.
024.
ImageView imgView;
025.
static
final
int
REQUEST_TAKE_PHOTO =
1
;
026.
String mCurrentPhotoPath;
027.
028.
static
String strSDCardPathName = Environment.getExternalStorageDirectory() +
"/temp_picture"
+
"/"
;
029.
030.
@Override
031.
public
void
onCreate(Bundle savedInstanceState) {
032.
super
.onCreate(savedInstanceState);
033.
setContentView(R.layout.activity_main);
034.
035.
036.
createFolder();
037.
038.
039.
imgView = (ImageView) findViewById(R.id.imgView);
040.
041.
042.
final
Button btnTakePhoto = (Button) findViewById(R.id.btnTakePhoto);
043.
044.
btnTakePhoto.setOnClickListener(
new
View.OnClickListener() {
045.
public
void
onClick(View v) {
046.
047.
Intent takePictureIntent =
new
Intent(MediaStore.ACTION_IMAGE_CAPTURE);
048.
049.
if
(takePictureIntent.resolveActivity(getPackageManager()) !=
null
) {
050.
051.
File photoFile =
null
;
052.
try
{
053.
photoFile = createImageFile();
054.
}
catch
(IOException ex) {}
055.
056.
if
(photoFile !=
null
) {
057.
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
058.
Uri.fromFile(photoFile));
059.
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
060.
}
061.
}
062.
063.
}
064.
});
065.
066.
}
067.
068.
069.
070.
private
File createImageFile()
throws
IOException {
071.
072.
String timeStamp =
new
SimpleDateFormat(
"yyyyMMdd_HHmmss"
).format(
new
Date());
073.
String imageFileName =
"JPEG_"
+ timeStamp +
"_"
;
074.
File storageDir =
new
File(strSDCardPathName);
075.
File image = File.createTempFile(
076.
imageFileName, /* prefix */
077.
".jpg"
,
078.
storageDir
079.
);
080.
081.
082.
mCurrentPhotoPath = image.getAbsolutePath();
083.
return
image;
084.
}
085.
086.
087.
@Override
088.
protected
void
onActivityResult(
int
requestCode,
int
resultCode, Intent data) {
089.
if
(resultCode == RESULT_OK) {
090.
Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath);
091.
imgView.setImageBitmap(bitmap);
092.
093.
094.
GalleryAddPic();
095.
}
096.
}
097.
098.
private
void
GalleryAddPic() {
099.
Intent mediaScanIntent =
new
Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
100.
File f =
new
File(mCurrentPhotoPath);
101.
Uri contentUri = Uri.fromFile(f);
102.
mediaScanIntent.setData(contentUri);
103.
this
.sendBroadcast(mediaScanIntent);
104.
}
105.
106.
public
static
void
createFolder()
107.
{
108.
File folder =
new
File(strSDCardPathName);
109.
try
110.
{
111.
112.
if
(!folder.exists()) {
113.
folder.mkdir();
114.
}
115.
}
catch
(Exception ex){}
116.
117.
}
118.
119.
@Override
120.
public
boolean
onCreateOptionsMenu(Menu menu) {
121.
getMenuInflater().inflate(R.menu.main, menu);
122.
return
true
;
123.
}
124.
125.
}