注意:三星手机中会出现照片旋转,进而引起返回数据时出现的activity生命周期被重置的问题,在清单文件中添加
一下代码就可以:
android:configChanges="orientation|keyboardHidden|screenSize"
/**
 * 从相册选择原生的照片(不裁切)
 */
public static void selectPhotoFromGallery(BrandCarBaseActivity activity, int requestCode) {Intent intent = new Intent();intent.setAction(Intent.ACTION_PICK);//从所有图片中进行选择
    intent.setType("image/*");activity.startActivityForResult(intent, requestCode);
}/**
 * 拍取照片不裁切
 */
public static void selectPhotoFromTake(BrandCarBaseActivity activity, String fiePath, int requestCode) {Intent intent = new Intent();//设置Action为拍照
    intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//将拍取的照片保存到指定URI
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(fiePath)));activity.startActivityForResult(intent, requestCode);
}/**
 * 采样率(等比)压缩
 * 根据路径获得图片并压缩,返回bitmap用于显示
 *
 * @param filePath  图片路径
 * @param reqHeight 照片压缩后的height
 * @param reqWidth  照片压缩后的width
 * @return
 */
public static Bitmap getCompressBitmap(String filePath, int reqHeight, int reqWidth) {final BitmapFactory.Options options = new BitmapFactory.Options();if (filePath != null) {options.inJustDecodeBounds = true;BitmapFactory.decodeFile(filePath, options);options.inSampleSize = caculateInSampleSize(options, reqHeight, reqWidth);options.inPreferredConfig = Bitmap.Config.RGB_565;options.inJustDecodeBounds = false;return rotateBitmapByDegree(BitmapFactory.decodeFile(filePath, options), getBitmapDegree(filePath));} else {return null;}
}/**
 * 采样率(等比)压缩
 *
 * @param uri     图片uri
 * @param context
 * @return
 */
public static Bitmap getCompressBitmapFromUri(Context context, Uri uri, int reqHeight, int reqWidth) {return getCompressBitmap(getPathByUri(context, uri), reqHeight, reqWidth);
}/**
 * 质量压缩并存储
 *
 * @param bitmap
 * @param outFilePath 输出路径
 * @param outSize     压缩后输出大小 kb
 */
public static void compressAndSaveImage(final Bitmap bitmap, final String outFilePath,final int outSize) {if (bitmap == null){return;}new Thread(new Runnable() {@Override
        public void run() {ByteArrayOutputStream baos = new ByteArrayOutputStream();int options = 100;//质量压缩方法,把压缩后的数据存放到baos中 (100表示不压缩,0表示压缩到最小)
            bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);// 循环判断如果压缩后图片是否大于100kb,大于继续压缩
            while (baos.toByteArray().length / 1024 > outSize) {// 重置baos即让下一次的写入覆盖之前的内容
                baos.reset();options -= 10;if (options < 0) {options = 0;}bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);if (options == 0) {break;}}try {FileOutputStream fos = new FileOutputStream(new File(outFilePath));fos.write(baos.toByteArray());fos.flush();fos.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}).start();
}/**
 * 质量压缩并存储
 *
 * @param bitmap
 * @param outFilePath 输出路径
 */
public static void saveImage(final Bitmap bitmap, final String outFilePath) {if (bitmap == null){return;}new Thread(new Runnable() {@Override
        public void run() {try {int options = 100;FileOutputStream fos = new FileOutputStream(new File(outFilePath));bitmap.compress(Bitmap.CompressFormat.JPEG, options, fos);fos.flush();fos.close();} catch (Exception e) {e.printStackTrace();}}}).start();
}/**
 * 计算图片缩放值
 *
 * @param options
 * @param reqWidth
 * @param reqHeight
 * @return
 */
public static int caculateInSampleSize(BitmapFactory.Options options, int reqHeight, int reqWidth) {final int width = options.outWidth;final int height = options.outHeight;int inSampleSize = 1;if (width > reqWidth || height > reqHeight) {final int heightRatio = Math.round((float) height / (float) reqHeight);final int widthRatio = Math.round((float) width / (float) reqWidth);inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;}return inSampleSize;
}public static String getPathByUri(Context context, Uri uri) {if ("content".equalsIgnoreCase(uri.getScheme())) {return getDataColumn(context, uri);} else {File file = new File(uri.getPath());if (file.exists()) {return uri.getPath();}}return null;
}/**
 * uri路径查询字段
 *
 * @param context
 * @param uri
 * @return
 */
public static String getDataColumn(Context context, Uri uri) {Cursor cursor = null;final String column = "_data";final String[] projection = {column};try {cursor = context.getContentResolver().query(uri, projection, null, null, null);if (cursor != null && cursor.moveToFirst()) {final int index = cursor.getColumnIndexOrThrow(column);return cursor.getString(index);}} finally {if (cursor != null)cursor.close();}return null;
}/**
 * 读取图片的旋转的角度
 *
 * @param path 图片绝对路径
 * @return 图片的旋转角度
 */
public static int getBitmapDegree(String path) {int degree = 0;try {// 从指定路径下读取图片,并获取其EXIF信息
        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;
}public static Bitmap rotateBitmapByDegree(Bitmap bm, int degree) {Bitmap returnBm = null;try {// 将原始图片按照旋转矩阵进行旋转,并得到新的图片
        if (bm != null) {// 根据旋转角度,生成旋转矩阵
            Matrix matrix = new Matrix();matrix.postRotate(degree);returnBm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);}} catch (OutOfMemoryError e) {}if (returnBm == null) {returnBm = bm;}if (bm != returnBm) {bm.recycle();}return returnBm;
}

获得相册或者拍照后数据处理方法://REQ_WIDTH和REQ_HEIGHT 是期望得到照片的大小
if (data.getData() == null) return;
if (data.getData().getPath().startsWith("file")) {toastMsg(getResourceString(R.string.please_select_native_pic));return;
}
Bitmap bm = getCompressBitmapFromUri(this, data.getData(), REQ_WIDTH, REQ_HEIGHT);

手机拍照或者相册获取图片总结相关推荐

  1. android开发 获取相册名称_android通过拍照、相册获取图片并显示 实例完整源码下载(亲测通过)...

    [实例简介]其中也包含了 将图片保存至 sd卡功能 [实例截图] [核心代码] public class MainActivity extends Activity{ private static f ...

  2. android相册路径地址,Android拍照和相册获取图片路径

    /** *调用系统相机 */ public voidstartCamera() { Intent intent =newIntent(MediaStore.ACTION_IMAGE_CAPTURE); ...

  3. 微信小程序-从相册获取图片,视频 使用相机拍照,录像上传+服务器(nodejs版)接收

    在本文 微信小程序-从相册获取图片 使用相机拍照 本地图片上传之前需要看看 微信小程序-获取用户session_key,openid,unionid - 后端为nodejs 代码封装是在上文添加的. ...

  4. 微信小程录制视频上传服务器,微信小程序-从相册获取图片,视频使用相机拍照,录像上传+服务器nodejs版接收-微信小程序视频上传功能-微信小程序视频上传...

    在本文微信小程序-从相册获取图片使用相机拍照本地图片上传之前需要看看微信小程序-获取用户session_key,openid,unionid-后端为nodejs代码封装是在上文添加的.本文知识点:1. ...

  5. Android 调用系统打开相机,打开相册获取图片路径

    我们在开发中经常遇到一些功能需要调取系统相机拍照获取图片,或者有的时候直接打开图库获取图片,那我们怎么获取呢,今天分享下, 第一步,打开相机 public static final int CAMER ...

  6. 关于小米手机从系统相册选择图片的一个bug

    在开发应用的时候碰到一个问题,拍照或者从系统相册选择图片并上传,首先从相机或者相册获取图片并压缩处理,最后再上传,代码写好并基本测试通过,然而深度测试的时候却发现有一个问题. 在小米手机,即MIUI系 ...

  7. iOS 从相机或相册获取图片并裁剪

    /load user image - (void)UesrImageClicked { UIActionSheet *sheet; // 判断是否支持相机 if([UIImagePickerContr ...

  8. 从相机相册获取图片裁剪后用于评论晒图或更换背景图

    这是我人生中写的第一篇博客,是否要纪念一下这一刻(2016.09.01 16:52).其实关于写博客,老早就有这种写法,首先觉得他能够帮我总结我学到的和用过的技术,其次还能帮助那些和我有一样需求的人, ...

  9. QRCode 扫描二维码、扫描条形码、相册获取图片后识别、生成带 Logo 二维码、支持微博微信 QQ 二维码扫描样式...

    QRCode 扫描二维码.扫描条形码.相册获取图片后识别.生成带 Logo 二维码.支持微博微信 QQ 二维码扫描样式 参考链接:https://github.com/bingoogolapple/B ...

最新文章

  1. Luna的大学读书史(1,Intro)
  2. Linux磁盘管理----分区格式化挂载fdisk、mkfs、mount
  3. 离散数学图论旅行规划问题_2020年MathorCup高校数学建模挑战赛——C 题 仓内拣货优化问题...
  4. AI应用开发实战系列之四 - 定制化视觉服务的使用
  5. php 栈实现历史记录后退,栈:如何实现浏览器的前进和后退功能
  6. linux之Ansible快速入门
  7. 光纤温度传感器在电力系统的应用
  8. 什么是收缩压和舒张压?
  9. java培训老师面试题_千锋Java培训老师分享Java实习生面试题
  10. k8s中的Secret
  11. Scaled-YOLOv4: Scaling Cross Stage Partial Network 论文翻译
  12. 金融业分布式数据库选型及HTAP场景实践
  13. 全球化手册|日本篇笔记
  14. php视频播放链提取,从PHP获取Vimeo的直接链接视频
  15. 数据要素产业链分析报告
  16. 从用户交互场景出发,欧瑞博MixPad要系统化定义智能居住空间
  17. 神经主题模型及应用(Neural Topic Model)
  18. 2019量子计算机股票,2019年中盘点:大战一触即发,PC市场已剑拔弩张
  19. 电路设计--驱动放大电路设计
  20. 如何做出漂亮的序列比对图——ENDscript/ESPript

热门文章

  1. 关于微信 调用js-sdk接口报错的问题
  2. 指针和引用的区别以及引用与指针基础
  3. 学习OpenCV:滤镜系列(11)——高反差保留 (6.30修改版)
  4. 漫步者蓝牙耳机w800x看油管视频自动关机问题解决办法
  5. ArcGIS制作区位图教程
  6. java 后端如何处理数据库字段类型为Json格式的方法
  7. 你如何检查参数的合法性?
  8. EC中的QEvent(SCI中断)
  9. 解决EAapp启动不了“战地V”等游戏的方法
  10. CStdioFile的用法详细解析