调用系统相机拍照获取图片或者从相册中调用本地图片显示问题
来源:互联网 发布:始作俑者 其无后乎 编辑:程序博客网 时间:2024/05/04 21:01
需求是这样的:打开系统或者第三方相机app拍照,然后使用所拍照片进行处理,或者直接调用系统或者第三方相册app,选择图片返回使用。需求很简单,过程很蛋疼,网上有很多方法,不过有很多不好用的地方,最后使用的这种方式还不错,记录一下,还是那句话,我很懒,以后复制使用直接复制代码:
1.调用相机拍照返回所拍照片(不是缩略图),点击按钮,调用相机:
Intent intent = new Intent(); intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE); File file = new File(getExternalFilesDir(Environment.DIRECTORY_DCIM) +"/bidata"); if (!file.exists()) { file.mkdirs(); } intent.putExtra(MediaStore.Images.Media.ORIENTATION, 0); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(file, "cameraTmp.png"))); startActivityForResult(intent, 0);获取所得照片:
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK) { switch (requestCode) { case 0: Bitmap bitmap = CommonUtils.getBitmap(this, getExternalFilesDir(Environment.DIRECTORY_DCIM) + "/bidata/"+ "cameraTmp.png"); break; } super.onActivityResult(requestCode, resultCode, data); }2.调用相册获取图片
Intent picture = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(picture, 1);获取所得到的图片:
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK) { switch (requestCode) { case 1: CommonUtils.bitmap = BitmapFactory.decodeFile(CommonUtils.getRealFilePath(this, data.getData())); break; } } super.onActivityResult(requestCode, resultCode, data); }最后把两个加载bitmap的方法贴出来:
/** * 由string类型的路径加载图片 * * @param context * @param path * @return */ public static Bitmap getBitmap(Context context, String path) { Uri uri = Uri.fromFile(new File(path)); InputStream in = null; try { in = context.getContentResolver().openInputStream(uri); // Decode image size BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; BitmapFactory.decodeStream(in, null, o); in.close(); int scale = 1; if (o.outHeight > 1024 || o.outWidth > 1024) { scale = (int) Math.pow( 2, (int) Math.round(Math.log(1024 / (double) Math.max( o.outHeight, o.outWidth)) / Math.log(0.5))); } if (scale < 4) { scale = 4; } BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inSampleSize = scale; in = context.getContentResolver().openInputStream(uri); Bitmap b = BitmapFactory.decodeStream(in, null, o2); in.close(); return b; } catch (FileNotFoundException e) { } catch (IOException e) { } return null; } /** * 将图片的uri转为绝对路径 * * @param context * @param uri * @return */ public static String getRealFilePath(final Context context, final Uri uri) { if (null == uri) return null; final String scheme = uri.getScheme(); String data = null; if (scheme == null) data = uri.getPath(); else if (ContentResolver.SCHEME_FILE.equals(scheme)) { data = uri.getPath(); } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) { Cursor cursor = context.getContentResolver().query(uri, new String[]{MediaStore.Images.ImageColumns.DATA}, null, null, null); if (null != cursor) { if (cursor.moveToFirst()) { int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); if (index > -1) { data = cursor.getString(index); } } cursor.close(); } } return data; }也 希望对有需要的战友有帮助。。。。
0 0
- 调用系统相机拍照获取图片或者从相册中调用本地图片显示问题
- android 调用系统相册选取照片或者打开相机拍照获取图片
- android 调用系统相册选取照片或者打开相机拍照获取图片
- 调用系统相机拍照,从相册选取图片上传
- Android开发,调用相册图片或者调用相机进行拍照获取图片
- 系统相机拍照、从相册中获取图片
- android中调用系统功能 来显示本地相册图片 拍照 视频 音频功能
- Android7.0调用系统相机拍照、相册选择图片、裁剪
- android中调用系统相机拍照并访问本地相册
- android4.4,6.0,7.0系统调用相机拍照通配及获取相册图片
- 从 相机 或者相册 获取图片显示在ImageView 上
- 从相机或者相册,获取图片显示在ImageView上
- Android 调用系统相机拍照(适配7.0)、从图库选择图片;从数据库读取联系人信息、相册图片
- android 调用系统相册选取照片或者打开相机拍照获取图片,返回时回到手机桌面,然后才会跳转到当前activity
- android 调用系统相册选取照片或者打开相机拍照获取图片,返回时回到手机桌面,然后才会跳转到当前activity
- Android中调用系统相机、系统相册来获取图片,并裁剪图片。
- 解决Android调用系统相机拍照后相片无法在相册中显示问题
- 完美调用系统相机拍照,获取图片进行压缩并本地保存
- 如何验证apk是否签名成功,apk比对keystore的签名信息
- 到处Can—加密和解密cookie(登录操作)
- Android Notification
- js事件
- Eclipse 反编译
- 调用系统相机拍照获取图片或者从相册中调用本地图片显示问题
- Build相关属性和调用系统信息的方法
- Mahout基于内存的DataMode 推荐引擎
- tomcat配置
- iOS 计算两个日期字符串的差值
- 《机器学习实战》ch2源码解析
- Android 之二维码
- Android动画学习总结
- POI取得Cell的值