android拍照保存被压缩
2016-02-02 10:31
330 查看
使用Intent调用系统的拍照功能时,
为了得到原始图片(未被压缩),给intent添加
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);,并用onActivityResult方法中的data得到的bitmap,实际是被压缩过的。
为了得到原始图片(未被压缩),给intent添加
intent.putExtra(MediaStore.EXTRA_OUTPUT,uri)属性,这种方式的过程就是将拍摄的图片存储到uri路径中,而onActivityResult只是负责显示照片,也就是说提前确定存储的路径。
btnPhoto.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); File out = new File(getPhotopath()); Uri uri = Uri.fromFile(out); intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); startActivityForResult(intent, SELECT_CAMER); } }); protected String getPhotopath() { String path = Environment.getExternalStorageDirectory()+"/my/" File file = new File(path); if(!file.exists()){ file.mkdir();// 创建文件夹 } String fileName = "test.jpg"; return path + fileName; } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode == SELECT_CAMER && resultCode == Activity.RESULT_OK){ Bitmap bitmap = getBitmapFromUrl(getPhotopath(), 300, 400); saveScalePhoto(bitmap); mImage.setImageBitmap(bitmap); } } /** * 根据路径获取图片资源 */ private Bitmap getBitmapFromUrl(String url, double width, double height) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; // 设置了此属性一定要记得将值设置为false Bitmap bitmap = BitmapFactory.decodeFile(url); // 防止OOM发生 options.inJustDecodeBounds = false; int mWidth = bitmap.getWidth(); int mHeight = bitmap.getHeight(); Matrix matrix = new Matrix(); float scaleWidth = 1; float scaleHeight = 1; // try { // ExifInterface exif = new ExifInterface(url); // String model = exif.getAttribute(ExifInterface.TAG_ORIENTATION); // } catch (IOException e) { // e.printStackTrace(); // } // 按照固定宽高进行缩放 // 这里希望知道照片是横屏拍摄还是竖屏拍摄 // 因为两种方式宽高不同,缩放效果就会不同 // 这里用了比较笨的方式 if(mWidth <= mHeight) { scaleWidth = (float) (width/mWidth); scaleHeight = (float) (height/mHeight); } else { scaleWidth = (float) (height/mWidth); scaleHeight = (float) (width/mHeight); } // matrix.postRotate(90); /* 翻转90度 */ // 按照固定大小对图片进行缩放 matrix.postScale(scaleWidth, scaleHeight); Bitmap newBitmap = Bitmap.createBitmap(bitmap, 0, 0, mWidth, mHeight, matrix, true); // 用完了记得回收 bitmap.recycle(); return newBitmap; } /** * 存储缩放的图片 * @param data 图片数据 */ private void saveScalePhoto(Bitmap bitmap) { // 照片全路径 String fileName = ""; // 文件夹路径 String pathUrl = Environment.getExternalStorageDirectory().getPath()+"/my/"; String imageName = "test.jpg"; FileOutputStream fos = null; File file = new File(pathUrl); file.mkdirs();// 创建文件夹 fileName = pathUrl + imageName; try { fos = new FileOutputStream(fileName); bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos); } catch (FileNotFoundException e) { e.printStackTrace(); } finally { try { fos.flush(); fos.close(); } catch (IOException e) { e.printStackTrace(); } } }
相关文章推荐
- Android自定义权限
- Android获取CPU使用率的几种方式
- Android 使用MaterialDesign Android Library会出现的问题
- Android 中ScrollView 只显示一行的问题
- android平铺图片
- canvas画各种图形
- Android开发
- android selector设置button点击效果(详细)以及常见问题
- Android查询 每个进程的权限
- 【入门篇(二)】ANDROID开发之BUG专讲
- Android自定义开关3
- Android JSON解析
- Android自定义控件2
- 浅析 Android 的窗口
- Android 解析XML格式数据
- android studio 2.0 preview 使用技巧
- 博客园—Android客户端
- Android 查看手机电池信息
- Android自定义控件1
- Android测试环境搭建(win7)