MediaStore这个类是android系统提供的一个多媒体数据库,android中多媒体信息都可以从这里提取。这个MediaStore包括了多媒体数据库的所有信息,包括音频,视频和图像,android把所有的多媒体数据库接口进行了封装,所有的数据库不用自己进行创建,直接调用利用ContentResolver去掉用那些封装好的接口就可以进行数据库的操作了
//调用系统相机
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
ContentValues values = new ContentValues();photoUri = getActivity().getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
values);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
photoUri);
startActivityForResult(intent, 2);
//调用系统相机
String path2 = pathFromPhoto();
BitmapFactory.Options options = new BitmapFactory.Options(); //通过BitmapFactory见图片路径转化成Bitmap格式
options.inJustDecodeBounds = true;
Bitmap bitmap2 = BitmapFactory.decodeFile(path2, options); // 此时返回
// bm
// 为空
options.inJustDecodeBounds = false; // 缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
int be = (int) (options.outHeight / (float) 320);
if (be <= 0)
be = 1;
// Bitmap将以options.inSampleSize的指数的倒数倍进行缩放
options.inSampleSize = be; // 重新读入图片,注意此时已经把
// options.inJustDecodeBounds 设回
// false 了
bitmap2 = BitmapFactory.decodeFile(path2, options);
post_Image(path2);
driver_header.setImageBitmap(bitmap2);
//获取到相关图片的路径
private String pathFromPhoto() {
String picPath = null;
String[] pojo = { MediaStore.Images.Media.DATA };
Cursor cursor = getActivity().managedQuery(photoUri, pojo, null, null,
null);
if (cursor != null) {
int columnIndex = cursor.getColumnIndexOrThrow(pojo[0]);
cursor.moveToFirst();
picPath = cursor.getString(columnIndex);
try {
if (Integer.parseInt(Build.VERSION.SDK) < 14) {
cursor.close();
}
} catch (NumberFormatException e) {
Log.v("qin", "error:" + e);
}
}
return picPath;
}