android调用系统相机的方法,并且解决三星手机遇到的问题,图片旋转、压缩
来源:互联网 发布:实用最优化方法答案 编辑:程序博客网 时间:2024/04/29 05:38
1.拍照完成之后直接存图片
- Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
- intent.putExtra("crop", "true");// crop=true 有这句才能出来最后的裁剪页面.
- intent.putExtra("aspectX", 5); // 这两项为裁剪框的比例.
- intent.putExtra("aspectY", 4);
- intent.putExtra("output", Uri.fromFile(new File("SDCard/1.jpg")));//输出地址
- intent.putExtra("outputFormat", "JPEG");//返回格式
- startActivityForResult(intent, REQUEST_CAMERA);
获取图片直接在onActivityForResult中
- BitmapFactory.decodeFile(fileName, opts);
2.拍照完成后读取intent中的数据,在onActivityForResult中
- Uri uri = data.getData();
- ContentResolver cr = this.getContentResolver();
- try {
- bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
但是这种方法在测试过程中,海信手机会报空指针异常,也许是相机处理机制的问题,我没有深究
3.最后一种是当前项目正用到的方法 目前在各种手机上使用正常
- String sdStatus = Environment.getExternalStorageState();
- if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用
- LogUtils.i("TestFile",
- "SD card is not avaiable/writeable right now.");
- return;
- }
- String name = new DateFormat().format(
- "yyyyMMdd_hhmmss",
- Calendar.getInstance(Locale.CHINA))
- + ".jpg";
- Bundle bundle = data.getExtras();
- Bitmap bitmap = (Bitmap) bundle.get("data");//
- LogUtils.d(CommitCommentActivity.this,
- bitmap == null ? "bitmap is null"
- : "bitmap is not null");
- FileOutputStream fos = null;
- File file = new File(IMAGE_PATH);
- file.mkdirs();// 创建文件夹
- final String fileName = IMAGE_PATH + name;
- LogUtils.i("fangdd", "Onresult path:" + fileName);
- try {
- fos = new FileOutputStream(fileName);
- bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);// 把数据写入文件
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } finally {
- if (!bitmap.isRecycled()) {
- bitmap.recycle();
- System.gc();
- }
- try {
- fos.flush();
- fos.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
这种方式是将图片资源取出之后存入文件,并不在activity中直接保存bitmap 仅在用到时加载 避免内存溢出
下面记录一下开发中遇到的问题,说道这里不得不吐槽一下三星的相机,调用之后系统会调用ondestroy导致activity被销毁重建,原因是系统调用了一次横竖屏切换,解决方法如下
方法一:刚开始调用了onSaveInstanceState(Bundle savedInstanceState)和onRestoreInstanceState(Bundle savedInstanceState)这个方法来实现,虽然方法比较笨,但是可以通过在onSaveInstanceState中保存一些你需要的变量,在onCreate()方法中判断savedInstanceState是否为null,不为null则调用onRestoreInstanceState()方法取出之前存的变量来使用,这相当于重新加载了一边当前activity。
方法二:上面的方法可行,但并不是解决问题的根本办法,后来通过查看发现在调用相机时,activity从竖屏切换到了横屏,在横竖屏切换导致了activity重新装载,找到根本原因后,在activity中通过Android:configChanges="orientation|keyboardHidden" 这个属性,可以约束调用相机时,保持当前activity竖屏状态不变,从而解决了activity重新加载的问题。
另外关于图片压缩的代码也一并贴在这里以便以后查看
- private Bitmap getDiskBitmap(String pathString) {
- Bitmap bitmap = null;
- Bitmap bMapRotate = null;
- try {
- File file = new File(pathString);
- if (file.exists()) {
- BitmapFactory.Options opt = new BitmapFactory.Options();
- opt.inPreferredConfig = Bitmap.Config.RGB_565;
- opt.inPurgeable = true;
- opt.inInputShareable = true;
- opt.inTempStorage = new byte[1024 * 1024 * 10];
- long length = file.length();
- LogUtils.d(this, "file.length() = " + length);
- if (length / (1024 * 1024) > 4) {
- opt.inSampleSize = 16;
- LogUtils.d(this, "opt.inSampleSize = 16;");
- } else if (length / (1024 * 1024) >= 1) {
- opt.inSampleSize = 8;
- LogUtils.d(this, "opt.inSampleSize = 8;");
- } else if (length / (1024 * 512) >= 1) {
- opt.inSampleSize = 4;
- LogUtils.d(this, "opt.inSampleSize = 4;");
- } else if (length / (1024 * 256) >= 1) {
- opt.inSampleSize = 2;
- LogUtils.d(this, "opt.inSampleSize = 2;");
- } else {
- opt.inSampleSize = 1;
- LogUtils.d(this, "opt.inSampleSize = 1;");
- }
- bitmap = BitmapFactory.decodeFile(pathString, opt);
- LogUtils.i("fangdd", "图片旋转度数:" + readPictureDegree(pathString));
- // ///////////
- // if(pathString.contains("floor/imgs")){
- // YLog.i("fangdd", "处理旋转:"+isGetImage);
- int orientation = readPictureDegree(pathString);
- /*
- * if(bitmap.getHeight() < bitmap.getWidth()){ orientation = 90;
- * } else { orientation = 0; }
- */
- if (orientation != 0) {
- Matrix matrix = new Matrix();
- matrix.postRotate(orientation);
- bMapRotate = Bitmap
- .createBitmap(bitmap, 0, 0, bitmap.getWidth(),
- bitmap.getHeight(), matrix, true);
- } else {
- bMapRotate = Bitmap.createScaledBitmap(bitmap,
- bitmap.getWidth(), bitmap.getHeight(), true);
- }
- // }
- // //////////////
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- if (bMapRotate != null) {
- return bMapRotate;
- }
- return bitmap;
- }
- /**
- * 读取图片属性:旋转的角度
- *
- * @param path
- * 图片绝对路径
- * @return degree旋转的角度
- */
- public static int readPictureDegree(String path) {
- int degree = 0;
- try {
- ExifInterface exifInterface = new ExifInterface(path);
- int orientation = exifInterface.getAttributeInt(
- ExifInterface.TAG_ORIENTATION,
- ExifInterface.ORIENTATION_NORMAL);
- switch (orientation) {
- case ExifInterface.ORIENTATION_ROTATE_90:
- degree = 90;
- break;
- case ExifInterface.ORIENTATION_ROTATE_180:
- degree = 180;
- break;
- case ExifInterface.ORIENTATION_ROTATE_270:
- degree = 270;
- break;
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- return degree;
- }
- android调用系统相机的方法,并且解决三星手机遇到的问题,图片旋转、压缩
- android调用系统相机的方法,并且解决三星手机遇到的问题
- 解决三星调用系统相机拍照显示图片旋转90度横着的问题
- Android解决三星手机图片旋转问题
- 关于三星手机调用系统相机拍照旋转屏导致图片数据丢失问题
- 三星手机 调用系统相机 data 为null 崩溃的问题 (未解决)
- Android中三星调用系统相机拍照后旋转的问题
- 关于imageloader的使用以及调用系统相机相片 三星手机上的旋转90度解决
- Android 调用系统相机拍照并且显示在相册中,以及中间可能会遇到的一些问题的解决
- 三星手机拍照,图片旋转问题的解决
- android调用系统(相机)的图片,并且返回
- Android解决三星手机拍照图片调用时逆时针旋转90度问题
- Android:处理调用系统相机照片被压缩问题,图片内容看不清的问题
- 一些手机图片压缩处理后会出现旋转问题(三星)的解决方法
- android解决三星手机拍照获取图片的问题
- 图片压缩保存 处理三星拍照图片旋转问题的部分方法
- 整理android调用系统相机的方法及遇到的问题
- 整理android调用系统相机的方法及遇到的问题
- 九度OJ1051
- Nginx负载均衡配置
- 别以为有https就安全了,这样做支付宝也能轻易被攻破!
- URL转码 js decodeURIComponent encodeURIComponent 函数
- centos7 升级 php 5.4 -> php5.6
- android调用系统相机的方法,并且解决三星手机遇到的问题,图片旋转、压缩
- log 日志统计
- http 状态码 504 502
- 在Ubuntu14.04上安装GMT5
- [机房练习赛4.5]note
- Java实现SHA-256加密的两种方式
- R-Studio安装
- Flink流计算编程--看看别人怎么用Session Window
- canvas学习笔记