打开相册

                    if (position == 0) {//打开相册Intent intent = new Intent();intent.addCategory(Intent.CATEGORY_OPENABLE);intent.setType("image/*");if (Build.VERSION.SDK_INT < 19) {intent.setAction(Intent.ACTION_GET_CONTENT);} else {intent.setAction(Intent.ACTION_OPEN_DOCUMENT);}startActivityForResult(intent, REQUEST_CODE_PICK);} else if (position == 1) {//打开相机Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//下面这句指定调用相机拍照后的照片存储的路径intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new                         File(Environment.getExternalStorageDirectory().getPath()+"/"+"table.jpg")));startActivityForResult(intent, REQUEST_CODE_CAMERA);}

选择图片后的回调

//相机相册的回调@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);if (requestCode == REQUEST_CODE_PICK && resultCode == RESULT_OK) {String path = GetPathFromUri.getPath(UploadSkillCertifyActivity.this, data.getData());//imgPath就是压缩后的图片String imgPath = BitmapUtil.compressImageUpload(path);} else if (requestCode == REQUEST_CODE_CAMERA && resultCode == RESULT_OK) {String cameraPath = Environment.getExternalStorageDirectory().getPath()+"/"+"table.jpg";//imgPath就是压缩后的图片String imgPath = BitmapUtil.compressImageUpload(path);}}

获取相册图片地址的工具类


public class GetPathFromUri {@SuppressLint("NewApi")public static String getPath(final Context context, final Uri uri) {final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;// DocumentProviderif (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {// ExternalStorageProviderif (isExternalStorageDocument(uri)) {final String docId = DocumentsContract.getDocumentId(uri);final String[] split = docId.split(":");final String type = split[0];if ("primary".equalsIgnoreCase(type)) {return Environment.getExternalStorageDirectory() + "/" + split[1];}// TODO handle non-primary volumes}// DownloadsProviderelse if (isDownloadsDocument(uri)) {final String id = DocumentsContract.getDocumentId(uri);final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));return getDataColumn(context, contentUri, null, null);}// MediaProviderelse if (isMediaDocument(uri)) {final String docId = DocumentsContract.getDocumentId(uri);final String[] split = docId.split(":");final String type = split[0];Uri contentUri = null;if ("image".equals(type)) {contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;} else if ("video".equals(type)) {contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;} else if ("audio".equals(type)) {contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;}final String selection = "_id=?";final String[] selectionArgs = new String[]{split[1]};return getDataColumn(context, contentUri, selection, selectionArgs);}}// MediaStore (and general)else if ("content".equalsIgnoreCase(uri.getScheme())) {return getDataColumn(context, uri, null, null);}// Fileelse if ("file".equalsIgnoreCase(uri.getScheme())) {return uri.getPath();}return null;}/*** Get the value of the data column for this Uri. This is useful for* MediaStore Uris, and other file-based ContentProviders.** @param context       The context.* @param uri           The Uri to query.* @param selection     (Optional) Filter used in the query.* @param selectionArgs (Optional) Selection arguments used in the query.* @return The value of the _data column, which is typically a file path.*/public static String getDataColumn(Context context, Uri uri, String selection,String[] selectionArgs) {Cursor cursor = null;final String column = "_data";final String[] projection = {column};try {cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,null);if (cursor != null && cursor.moveToFirst()) {final int column_index = cursor.getColumnIndexOrThrow(column);return cursor.getString(column_index);}} finally {if (cursor != null)cursor.close();}return null;}/*** @param uri The Uri to check.* @return Whether the Uri authority is ExternalStorageProvider.*/public static boolean isExternalStorageDocument(Uri uri) {return "com.android.externalstorage.documents".equals(uri.getAuthority());}/*** @param uri The Uri to check.* @return Whether the Uri authority is DownloadsProvider.*/public static boolean isDownloadsDocument(Uri uri) {return "com.android.providers.downloads.documents".equals(uri.getAuthority());}/*** @param uri The Uri to check.* @return Whether the Uri authority is MediaProvider.*/public static boolean isMediaDocument(Uri uri) {return "com.android.providers.media.documents".equals(uri.getAuthority());}
}

图片压缩


public class BitmapUtil {/*** 质量压缩方法** @param image* @return*/private static Bitmap compressImage(Bitmap image) {ByteArrayOutputStream baos = new ByteArrayOutputStream();image.compress(Bitmap.CompressFormat.JPEG, 100, baos);int options = 100;while (baos.toByteArray().length / 1024 > 100) {baos.reset();image.compress(Bitmap.CompressFormat.JPEG, options, baos);options -= 10;}ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);return bitmap;}/*** 图片按比例大小压缩方法(根据路径获取图片并压缩)** @param srcPath* @return*/private static Bitmap getImage(String srcPath) {BitmapFactory.Options newOpts = new BitmapFactory.Options();newOpts.inJustDecodeBounds = true;Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);// 此时返回bm为空newOpts.inJustDecodeBounds = false;int w = newOpts.outWidth;int h = newOpts.outHeight;// 现在主流手机比较多是800*480分辨率,所以高和宽我们设置为float hh = 800f;// 这里设置高度为800ffloat ww = 480f;// 这里设置宽度为480f// 缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可int be = 1;// be=1表示不缩放if (w > h && w > ww) {// 如果宽度大的话根据宽度固定大小缩放be = (int) (newOpts.outWidth / ww);} else if (w < h && h > hh) {// 如果高度高的话根据宽度固定大小缩放be = (int) (newOpts.outHeight / hh);}if (be <= 0)be = 1;newOpts.inSampleSize = be;// 设置缩放比例// 重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了bitmap = BitmapFactory.decodeFile(srcPath, newOpts);return compressImage(bitmap);// 压缩好比例大小后再进行质量压缩}/*** 将压缩的bitmap保存到SDCard卡临时文件夹,用于上传** @param filename* @param bit* @return*/private static String saveMyBitmap(String filename, Bitmap bit) {String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath()+"/laopai/";String filePath = baseDir + filename;File dir = new File(baseDir);if (!dir.exists()) {dir.mkdir();}File f = new File(filePath);try {f.createNewFile();FileOutputStream fOut = null;fOut = new FileOutputStream(f);bit.compress(Bitmap.CompressFormat.PNG, 100, fOut);fOut.flush();fOut.close();} catch (IOException e1) {e1.printStackTrace();}return filePath;}/*** 压缩上传路径* @param path* @return*/public static String compressImageUpload(String path) {String filename = path.substring(path.lastIndexOf("/") + 1);Bitmap image = getImage(path);return saveMyBitmap(filename, image);}/*** 清除缓存文件*/public static void deleteCacheFile(){File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/laopai/");RecursionDeleteFile(file);}/*** 递归删除*/private static void RecursionDeleteFile(File file){if(file.isFile()){file.delete();return;}if(file.isDirectory()){File[] childFile = file.listFiles();if(childFile == null || childFile.length == 0){file.delete();return;}for(File f : childFile){RecursionDeleteFile(f);}file.delete();}}
}

解决oppo、vivo手机从相册选择图片获取图片地址问题相关推荐

  1. 从oppo vivo手机开始散谈

    前言 当时看到oppo,vivo拿下第三季度的国内手机销量排行榜的冠军和亚军,心里有许多想法,就写下这个题目后,一直有事没有将这文章完结,今天来继续聊聊这个话题吧. 那让我们先从一个小妹妹买手机说起吧 ...

  2. 4个顶级的华为/小米/OPPO/Vivo手机屏幕解锁工具软件

    有好几次用户发现自己被锁定在他们的华为/小米/OPPO/Vivo设备之外,我们知道这可能是一种非常可怕的体验.在这种情况下,找到安卓手机解锁软件,重新获得手机中重要数据和文件的访问权限.看看这篇文章, ...

  3. 微信浏览器返回刷新,监听微信浏览器返回事件,网页防复制,移动端禁止图片长按和vivo手机点击img标签放大图片

    以下代码都经过iphone7,华为MT7 ,谷歌浏览器,微信开发者工具,PC端微信验证.如有bug,还请在评论区留言. demo链接:https://pan.baidu.com/s/1c35mbjM ...

  4. Python如何不加载图片获取图片的分辨率(即尺寸,宽和高)?

    Python如何不加载图片获取图片的分辨率(即尺寸,宽和高)? 1.软件环境⚙️ 2.问题描述

  5. 小米手机从相册选择图片问题

    从相册选择图片,我从标题栏的菜单项上进行点击操作 @Override public boolean onOptionsItemSelected(MenuItem item) {if (item.get ...

  6. php拍照从手机相册中选择,Android获取图片:拍照和从相册中选择

    概述 在Android开发中获取图片主要包括如下两种方式: 打开相机拍照 从图库中选择图片 一.打开相机拍照 打开相机拍照主要包括如下几个部分: 权限申请 打开摄像头 拍照后传回数据处理 1. 权限申 ...

  7. 小米手机相册选择并裁剪图片

    /** * 裁剪原始的图片 */ public static final int PHOTOZOOM = 2; // 缩放 public static final int PHOTORESOULT = ...

  8. 如何解决部分iphone手机不识别webp格式的图片的问题

    部分iphone手机不识别webp格式的图片 最好的解决方式是找到后台让其修改图片的格式 要想自己修改之后正常显示,得确保有一个可替换的jpg文件 具体操作如下: goods_introduce: r ...

  9. Android打开相册vs拍照获取图片的原理实现

    前言:这几天在做用户登陆注册的逻辑时,遇到了要修改用户的头像问题的解决.在此把实现的原理以及实现过程中遇到的问题分享个大家...留下些许脚印 在手机的app里我们常常可用看到在个人中心页面有修改头像的 ...

最新文章

  1. Jquery empty() remove() detach() 方法的区别
  2. 基于canoe 新建一个lin工程_CANoe教程 | 高级编程 - C Library API
  3. responsebody如何将数据转换成json的_干货分享:如何用Retrofit直接获得Json数据(字符串)...
  4. android webview 多文件上传,Android中的webview支持页面中的文件上传实例代码
  5. Java并发编程举例Runnable, Callable, Future, FutureTask, CompletionService
  6. mysql 查询不为0的数据_查询数据库中所有记录总数不为0的数据表名称
  7. Qt文档阅读笔记-Simple Anchor Layout Example解析
  8. python的shelve库
  9. Qt + 运动控制 (固高运动控制卡)【1】环境准备,框架搭建
  10. Fragstats运行内存不够或卡顿问题解决
  11. 源码智造编辑器客户端v1.0.0 官方版
  12. 【网络安全】SQL注入详细分析
  13. 【数据结构】——各种树的定义
  14. 基于等级保护梳理服务器安全合规基线
  15. 原生M1/M2 Photoshop 2022 for Mac(PS2022)v23.4.2 中英文正式发布详情介绍安装教程
  16. python如何获取列表的长度
  17. VS2008 Debug Error R6034
  18. Java爬取B站弹幕 —— Python云图Wordcloud生成弹幕词云
  19. isam2 优化pose graph
  20. 太恐怖!黑客能把你的耳机变成麦克风用来监听

热门文章

  1. SDNU 1014 书的页码
  2. 安装和使用pyv8解析JavaScript
  3. ptp精准时间协议_PTP精确时钟同步协议概述及应用
  4. 为什么这么火?用 Python 爬取并分析了《雪中悍刀行》数据,发现了其中的秘密
  5. 如何判断list、map集合是否为空
  6. APP性能测试,你需要关注哪些指标?
  7. 华为p6android os耗电严重,华为手机耗电严重?那你肯定不知道这4个技巧,彻底解决...
  8. android 华为拍照功能介绍,华为手机拍照方式有哪些?华为手机六大拍照方式介绍...
  9. 日新进用户200W+,解密《龙之谷》手游背后的压测故事
  10. 如何提高学生对计算机英语的兴趣论文,英语教学计算机专业论文