android:layout_width=
"match_parent"
android:layout_height=
"match_parent"
android:orientation=
"vertical"
>
<Button
android:id=
"@+id/take_photo"
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
android:text=
"Take Photo"
/>
<Button
android:id=
"@+id/get_photo"
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
android:text=
"get Photo"
/>
<ImageView
android:id=
"@+id/picture"
android:layout_width=
"300dp"
android:layout_height=
"300dp"
android:layout_gravity=
"center_horizontal"
/>
</LinearLayout>
package
com.example.choosepictest;
import
java.io.File;
import
java.io.IOException;
import
java.text.SimpleDateFormat;
import
java.util.Date;
import
android.app.Activity;
import
android.content.Intent;
import
android.graphics.Bitmap;
import
android.graphics.BitmapFactory;
import
android.net.Uri;
import
android.os.Bundle;
import
android.os.Environment;
import
android.provider.MediaStore;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.Button;
import
android.widget.ImageView;
public
class
MainActivity
extends
Activity
implements
OnClickListener {
static
final
int
REQUEST_IMAGE_CAPTURE =
1
;
private
Button takePhoto;
private
Button getPhoto;
private
ImageView picture;
private
Uri imgUri;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
takePhoto = (Button) findViewById(R.id.take_photo);
getPhoto = (Button) findViewById(R.id.get_photo);
picture = (ImageView) findViewById(R.id.picture);
takePhoto.setOnClickListener(
this
);
getPhoto.setOnClickListener(
this
);
}
@Override
public
void
onClick(View v) {
switch
(v.getId()) {
case
R.id.take_photo:
dispatchTakePictureIntent();
break
;
default
:
break
;
}
}
String mCurrentPhotoPath;
private
void
dispatchTakePictureIntent() {
File appDir =
new
File(Environment.getExternalStorageDirectory(),
"/etoury/picCache"
);
if
(!appDir.exists()) {
appDir.mkdir();
}
String timeStamp =
new
SimpleDateFormat(
"yyyyMMdd_HHmmss"
)
.format(
new
Date());
String fileName = timeStamp +
".jpg"
;
File outputImage =
new
File(appDir, fileName);
try
{
if
(outputImage.exists()) {
outputImage.delete();
}
outputImage.createNewFile();
}
catch
(IOException e) {
e.printStackTrace();
}
mCurrentPhotoPath = outputImage.getAbsolutePath();
imgUri = Uri.fromFile(outputImage);
Intent intent =
new
Intent(
"android.media.action.IMAGE_CAPTURE"
);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imgUri);
if
(intent.resolveActivity(getPackageManager()) !=
null
) {
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
}
}
private
void
setPic() {
int
targetW = picture.getWidth();
int
targetH = picture.getHeight();
BitmapFactory.Options bmOptions =
new
BitmapFactory.Options();
bmOptions.inJustDecodeBounds =
true
;
BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
int
photoW = bmOptions.outWidth;
int
photoH = bmOptions.outHeight;
int
scaleFactor = Math.min(photoW/targetW, photoH/targetH);
bmOptions.inJustDecodeBounds =
false
;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable =
true
;
Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
picture.setImageBitmap(bitmap);
}
@Override
protected
void
onActivityResult(
int
requestCode,
int
resultCode, Intent data) {
if
(resultCode == RESULT_OK) {
switch
(requestCode) {
case
REQUEST_IMAGE_CAPTURE:
setPic();
break
;
default
:
break
;
}
}
}
}