public class ShootMainActivity extends Activity implements OnClickListener {
// 用来标识请求照相功能的activity
private static final int CAMERA_WITH_DATA = 3020;
// 用来标识请求gallery的activity
private static final int PHOTO_PICKED_WITH_DATA = 3021;
private File mCurrentPhotoFile;// 照相机拍照得到的图片
private int DECIDE;
private Button mPhotoButton; // 拍照
private Button mAlbumButton; // 相册
private ImageView mImage;
private Uri mImageUri;
private Uri uri;
private byte[] mBuffer; // 保存拍照的字节信息
private boolean hasPic = false;
private static final File PHOTO_DIR = new File(
Environment.getExternalStorageDirectory() + "/DCIM/Camera");
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initUI();
}
private void initUI() {
mPhotoButton = (Button) this.findViewById(R.id.photobutton);
mPhotoButton.setOnClickListener(this);
mAlbumButton = (Button) this.findViewById(R.id.albumbutton);
mAlbumButton.setOnClickListener(this);
mImage = (ImageView) this.findViewById(R.id.image);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.photobutton:
doTakePhoto();
break;
case R.id.albumbutton:
doPickPhotoFromGallery();
break;
}
}
// 拍照的照片存储位置
public String getPhotoDir() {
return Environment.getExternalStorageDirectory() + "/DCIM/Camera/1.jpg";
}
// 拍照获取图片
protected void doTakePhoto() {
PHOTO_DIR.mkdirs();
mCurrentPhotoFile = new File(getPhotoDir());// 给新照的照片文件命名
final Intent mIntent = getPhotoIntent(mCurrentPhotoFile);
startActivityForResult(mIntent, CAMERA_WITH_DATA);
}
// 请求Gallery程序
protected void doPickPhotoFromGallery() {
final Intent mIntent = getPhotoAlbumIntent();
startActivityForResult(mIntent, PHOTO_PICKED_WITH_DATA);
}
// 封装请求拍照的intent
public static Intent getPhotoIntent(File f) {
// Intent intent = new Intent("android.media.action.IMAGE_CAPTURE",
// null);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE, null);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
Bundle bundle = new Bundle();
bundle.putString("cascde", "19921231010");
intent.putExtras(bundle);
return intent;
}
// 封装请求Gallery的intent
public static Intent getPhotoAlbumIntent() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
// intent.putExtra("crop", "true");
// intent.putExtra("aspectX", 1);
// intent.putExtra("aspectY", 1);
// intent.putExtra("outputX", 80);
// intent.putExtra("outputY", 80);
intent.putExtra("return-data", true);
return intent;
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case CAMERA_WITH_DATA:
imageByteData(data);
break;
case PHOTO_PICKED_WITH_DATA:
imageUri(data);
break;
}
imageData();
}
private void imageData() {
if (!hasPic)
return;
else {
switch (DECIDE) {
case CAMERA_WITH_DATA:
Bitmap mbBitmap = getUri2Bitmap(uri);
// mImage.setImageURI(uri);
mImage.setImageBitmap(mbBitmap);
break;
case PHOTO_PICKED_WITH_DATA:
mImage.setImageURI(mImageUri);
break;
}
}
}
// 将uri转成bitmap
private Bitmap getUri2Bitmap(Uri uriTo) {
ContentResolver resolver = getContentResolver();
try {
// 将图片内容解析成字节数组
mBuffer = readStream(resolver.openInputStream(uriTo));
} catch (Exception e) {
e.printStackTrace();
}
// 将字节数组转换为ImageView可调用的Bitmap对象
Bitmap mBitmap = getPicFromBytes(mBuffer, null);
return mBitmap;
}
// 将字节数组转换为ImageView可调用的Bitmap对象
public static Bitmap getPicFromBytes(byte[] bytes,
BitmapFactory.Options opts) {
if (bytes != null)
if (opts != null)
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length,
opts);
else
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
return null;
}
// 将图片内容解析成字节数组
public static byte[] readStream(InputStream inStream) throws Exception {
byte[] buffer = new byte[1024];
int len = -1;
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
byte[] data = outStream.toByteArray();
outStream.close();
inStream.close();
return data;
}
private void imageUri(Intent data) {
mImageUri = data.getData();
this.hasPic = true;
this.DECIDE = PHOTO_PICKED_WITH_DATA;
}
public void imageByteData(Intent data) {
if (mCurrentPhotoFile == null)
mCurrentPhotoFile = new File(getPhotoDir());
uri = Uri.fromFile(mCurrentPhotoFile);
this.hasPic = true;
this.DECIDE = CAMERA_WITH_DATA;
}
}