android调用系统相机的方法,并且解决三星手机遇到的问题,图片旋转、压缩

在项目中遇到需要调用系统相机的功能点,整理方法如下

1.拍照完成之后直接存图片

[java]  view plain copy
  1.    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
  2.    intent.putExtra("crop""true");// crop=true 有这句才能出来最后的裁剪页面.  
  3.    intent.putExtra("aspectX"5); // 这两项为裁剪框的比例.  
  4.    intent.putExtra("aspectY"4);  
  5.      
  6.    intent.putExtra("output", Uri.fromFile(new File("SDCard/1.jpg")));//输出地址  
  7.    intent.putExtra("outputFormat""JPEG");//返回格式   
  8.    startActivityForResult(intent, REQUEST_CAMERA);  


获取图片直接在onActivityForResult中

[java]  view plain copy
  1. BitmapFactory.decodeFile(fileName, opts);  

 

2.拍照完成后读取intent中的数据,在onActivityForResult中

[java]  view plain copy
  1. Uri uri = data.getData();  
  2. ContentResolver cr = this.getContentResolver();  
  3. try {  
  4.     bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));  
  5. catch (FileNotFoundException e) {  
  6.     e.printStackTrace();  
  7. }  

但是这种方法在测试过程中,海信手机会报空指针异常,也许是相机处理机制的问题,我没有深究

3.最后一种是当前项目正用到的方法 目前在各种手机上使用正常

[java]  view plain copy
  1. String sdStatus = Environment.getExternalStorageState();  
  2.       if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用  
  3.        LogUtils.i("TestFile",  
  4.          "SD card is not avaiable/writeable right now.");  
  5.        return;  
  6.       }  
  7.       String name = new DateFormat().format(  
  8.         "yyyyMMdd_hhmmss",  
  9.         Calendar.getInstance(Locale.CHINA))  
  10.         + ".jpg";  
  11.       Bundle bundle = data.getExtras();  
  12.       Bitmap bitmap = (Bitmap) bundle.get("data");//  
  13.       LogUtils.d(CommitCommentActivity.this,  
  14.         bitmap == null ? "bitmap is null"  
  15.           : "bitmap is not null");  
  16.       FileOutputStream fos = null;  
  17.       File file = new File(IMAGE_PATH);  
  18.       file.mkdirs();// 创建文件夹  
  19.       final String fileName = IMAGE_PATH + name;  
  20.       LogUtils.i("fangdd""Onresult path:" + fileName);  
  21.       try {  
  22.        fos = new FileOutputStream(fileName);  
  23.        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);// 把数据写入文件  
  24.       } catch (FileNotFoundException e) {  
  25.        e.printStackTrace();  
  26.       } finally {  
  27.        if (!bitmap.isRecycled()) {  
  28.         bitmap.recycle();  
  29.         System.gc();  
  30.        }  
  31.        try {  
  32.         fos.flush();  
  33.         fos.close();  
  34.        } catch (IOException e) {  
  35.         e.printStackTrace();  
  36.        }  
  37.       }  

 这种方式是将图片资源取出之后存入文件,并不在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重新加载的问题。

 

 另外关于图片压缩的代码也一并贴在这里以便以后查看

[java]  view plain copy
  1. private Bitmap getDiskBitmap(String pathString) {  
  2.         Bitmap bitmap = null;  
  3.         Bitmap bMapRotate = null;  
  4.         try {  
  5.             File file = new File(pathString);  
  6.             if (file.exists()) {  
  7.                 BitmapFactory.Options opt = new BitmapFactory.Options();  
  8.                 opt.inPreferredConfig = Bitmap.Config.RGB_565;  
  9.                 opt.inPurgeable = true;  
  10.                 opt.inInputShareable = true;  
  11.                 opt.inTempStorage = new byte[1024 * 1024 * 10];  
  12.                 long length = file.length();  
  13.                 LogUtils.d(this"file.length() = " + length);  
  14.                 if (length / (1024 * 1024) > 4) {  
  15.                     opt.inSampleSize = 16;  
  16.                     LogUtils.d(this"opt.inSampleSize = 16;");  
  17.                 } else if (length / (1024 * 1024) >= 1) {  
  18.                     opt.inSampleSize = 8;  
  19.                     LogUtils.d(this"opt.inSampleSize = 8;");  
  20.                 } else if (length / (1024 * 512) >= 1) {  
  21.                     opt.inSampleSize = 4;  
  22.                     LogUtils.d(this"opt.inSampleSize = 4;");  
  23.                 } else if (length / (1024 * 256) >= 1) {  
  24.                     opt.inSampleSize = 2;  
  25.                     LogUtils.d(this"opt.inSampleSize = 2;");  
  26.                 } else {  
  27.                     opt.inSampleSize = 1;  
  28.                     LogUtils.d(this"opt.inSampleSize = 1;");  
  29.                 }  
  30.                 bitmap = BitmapFactory.decodeFile(pathString, opt);  
  31.                 LogUtils.i("fangdd""图片旋转度数:" + readPictureDegree(pathString));  
  32.                 // ///  
  33.                 // if(pathString.contains("floor/imgs")){  
  34.                 // YLog.i("fangdd", "处理旋转:"+isGetImage);  
  35.                 int orientation = readPictureDegree(pathString);  
  36.                 /* 
  37.                  * if(bitmap.getHeight() < bitmap.getWidth()){ orientation = 90; 
  38.                  * } else { orientation = 0; } 
  39.                  */  
  40.                 if (orientation != 0) {  
  41.                     Matrix matrix = new Matrix();  
  42.                     matrix.postRotate(orientation);  
  43.                     bMapRotate = Bitmap  
  44.                             .createBitmap(bitmap, 00, bitmap.getWidth(),  
  45.                                     bitmap.getHeight(), matrix, true);  
  46.                 } else {  
  47.                     bMapRotate = Bitmap.createScaledBitmap(bitmap,  
  48.                             bitmap.getWidth(), bitmap.getHeight(), true);  
  49.                 }  
  50.                 // }  
  51.                 // //  
  52.             }  
  53.         } catch (Exception e) {  
  54.             e.printStackTrace();  
  55.         }  
  56.         if (bMapRotate != null) {  
  57.             return bMapRotate;  
  58.         }  
  59.         return bitmap;  
  60.     }  
  61.   
  62.   
  63.     /** 
  64.      * 读取图片属性:旋转的角度 
  65.      *  
  66.      * @param path 
  67.      *            图片绝对路径 
  68.      * @return degree旋转的角度 
  69.      */  
  70.     public static int readPictureDegree(String path) {  
  71.         int degree = 0;  
  72.         try {  
  73.             ExifInterface exifInterface = new ExifInterface(path);  
  74.             int orientation = exifInterface.getAttributeInt(  
  75.                     ExifInterface.TAG_ORIENTATION,  
  76.                     ExifInterface.ORIENTATION_NORMAL);  
  77.             switch (orientation) {  
  78.             case ExifInterface.ORIENTATION_ROTATE_90:  
  79.                 degree = 90;  
  80.                 break;  
  81.             case ExifInterface.ORIENTATION_ROTATE_180:  
  82.                 degree = 180;  
  83.                 break;  
  84.             case ExifInterface.ORIENTATION_ROTATE_270:  
  85.                 degree = 270;  
  86.                 break;  
  87.             }  
  88.         } catch (IOException e) {  
  89.             e.printStackTrace();  
  90.         }  
  91.         return degree;  
  92.     }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值