拍照上传、相册获取逻辑实现
来源:互联网 发布:关于51单片机与esp8266 编辑:程序博客网 时间:2024/06/05 14:35
最近项目中用到的,做个笔记备份一下,so,请往下看~
- 相册及拍照上传部分
/** * @Description 打开系统相册 * @param null * @return void * @throws */ private void album2Photo() { Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(intent, SYS_INTENT_REQUEST); } /** * @Description 调用相机拍照 * @param null * @return void * @throws */ private void camera2Photo() { String sdStatus = Environment.getExternalStorageState(); /* 检测sd是否可用 */ if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { ToolUtils.showToast("SD卡不可用!"); return; } Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); startActivityForResult(intent, CAMERA_INTENT_REQUEST); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == SYS_INTENT_REQUEST && resultCode == RESULT_OK && data != null) { try { Uri uri = data.getData(); Cursor cursor = getContentResolver().query(uri, null, null, null, null); cursor.moveToFirst(); String imageFilePath = cursor.getString(1); Log.i("yzh","File path is----->" + imageFilePath); FileInputStream fis = new FileInputStream(imageFilePath); bitmap = BitmapFactory.decodeStream(fis); int width = bitmap.getWidth(); int height = bitmap.getHeight(); Log.i("yzh","压缩前的宽高----> width: " + width + " height:" + height); /* 压缩获取的图像 */ account_headshot_img.setImageBitmap(compressBigBitmap(bitmap, false)); fis.close(); cursor.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } else if (requestCode == CAMERA_INTENT_REQUEST && resultCode == RESULT_OK && data != null) { getPhoto4Camera(data); } super.onActivityResult(requestCode, resultCode, data); } /** * @Description 获取压缩后的bitmap,避免图片过大 * @param Bitmap;boolean * @return Bitmap * @throws */ private Bitmap compressBigBitmap(Bitmap bitmap, boolean isCamera) { Bitmap destBitmap = null; /* 图片宽度调整为100,大于这个比例的,按一定比例缩放到宽度为100 */ if (bitmap.getWidth() > 80) { float scaleValue = (float) (80f / bitmap.getWidth()); Log.i("yzh", "缩放比例:"+scaleValue); Matrix matrix = new Matrix(); /* 针对系统拍照,旋转90° 避免横屏*/ if (isCamera){ //matrix.setRotate(90); } matrix.postScale(scaleValue, scaleValue); destBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); int widthTemp = destBitmap.getWidth(); int heightTemp = destBitmap.getHeight(); Log.i("yzh", "压缩后的宽高----> width: " + heightTemp + " height:" + widthTemp); } else { return bitmap; } return destBitmap; } /** * @Description 拍照后获取照片 * @param Intent * @return void * @throws */ private void getPhoto4Camera(Intent data) { SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmssSSS"); String name = formatter.format(System.currentTimeMillis()) + ".jpg"; Log.i("yzh", "image name:" + name); Bundle bundle = data.getExtras(); /* 获取相机返回的数据,并转换为Bitmap图片格式 */ Bitmap bitmap = (Bitmap) bundle.get("data"); FileOutputStream fileOut = null; String path = Environment.getExternalStorageDirectory().getPath(); File file = new File(path + "/SCZW/"); /** 检测文件夹是否存在,不存在则创建文件夹 **/ if (!file.exists() && !file.isDirectory()) file.mkdirs(); String fileName = file.getPath() + "/" + name; Log.i("yzh", "camera file path:" + fileName); try { fileOut = new FileOutputStream(fileName); /* 把数据写入文件 */ bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOut); } catch (FileNotFoundException e) { e.printStackTrace(); } finally { try { if (fileOut == null) return; fileOut.flush(); fileOut.close(); } catch (IOException e) { e.printStackTrace(); } } account_headshot_img.setImageBitmap(compressBigBitmap(bitmap, true)); }
0 0
- 拍照上传、相册获取逻辑实现
- Android拍照或相册获取照片上传
- 上传头像 拍照 相册
- Android 拍照剪裁,获取相册图片,上传照片至服务端
- 拍照,和获取相册
- Android实现图片(拍照+相册)上传功能
- 带你实现拍照||从相册选择->裁剪->上传服务器
- input file 实现拍照上传和选择相册等文件
- android 拍照,图库,相册,上传
- Android打开相册vs拍照获取图片的原理实现
- Android 拍照、从相册获取及裁剪的相关实现
- 最简单实现从相册和拍照中获取图片
- Android实现拍照及从相册获取图片
- Android打开相册vs拍照获取图片的原理实现
- js实现获取手机相册并上传
- iOS 上传照片到服务器 获取相册和拍照功能的照片 照片压缩上传
- 手机拍照与相册获取
- 从相册\拍照获取图片
- CF#333(Div2) C. The Two Routes(最短路)
- i = i++
- Bootstrap入门基础(三)
- javascript的window.ActiveXObject对象,区别浏览器的方法
- 第七章.复用类 初始化和类的加载 总结
- 拍照上传、相册获取逻辑实现
- POJ 1679 The Unique MST 次小生成树
- 【Poj】-2253-Frogger(最短路,变化)
- 332. Reconstruct Itinerary(重要)
- 【HDU 2063 过山车】
- 数据结构实验之排序二:交换排序
- 前端-笔试面试知识点
- hdu5810Balls and Boxes+数学期望
- 英语信件常用句式