android 调用相机获取图片


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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值