从路径uri加载Bitmap,缩小图片到指定大小的方法记录
来源:互联网 发布:11年总决赛数据 编辑:程序博客网 时间:2024/05/16 12:55
根据uri获取实际的文件路径,位于工具类 @TargetApi(Build.VERSION_CODES.KITKAT) public static String getRealPathFromURI(Uri uri) { final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; // DocumentProvider if (isKitKat && DocumentsContract.isDocumentUri(context_, uri)) { // ExternalStorageProvider if (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];// } } // DownloadsProvider else 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); } // MediaProvider else 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 the remote address if (isGooglePhotosUri(uri)) return uri.getLastPathSegment(); return getDataColumn(context_, uri, null, null); } // File else if ("file".equalsIgnoreCase(uri.getScheme())) { return uri.getPath(); } return ""; } 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 index = cursor.getColumnIndexOrThrow(column); return cursor.getString(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()); } /** * @param uri The Uri to check. * @return Whether the Uri authority is Google Photos. */ public static boolean isGooglePhotosUri(Uri uri) { return "com.google.android.apps.photos.content".equals(uri.getAuthority()); }
加载时第一次缩放主要是为了缩小图片,节约加载内存 public Bitmap resizeImage1(String path,int dstWidth, int dstHeight) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true;//不加载bitmap到内存中 BitmapFactory.decodeFile(path, options); int srcWidth = options.outWidth; int srcHeight = options.outHeight; options.inDither = false; options.inPreferredConfig = Bitmap.Config.RGB_565; options.inSampleSize = 1; int inSampleSize = 1; if (srcWidth != 0 && srcHeight != 0 && dstWidth != 0 && dstHeight != 0) { if (srcHeight > dstHeight || srcWidth > dstWidth) { if (srcWidth > srcHeight) { inSampleSize = Math.round((float) srcHeight / (float) dstHeight); } else { inSampleSize = Math.round((float) srcWidth / (float) dstWidth); } }// inSampleSize的值只有为2的指数幂时才有效如1,2,4,6,这里宁愿多压缩一点节约内存所以加3 options.inSampleSize = inSampleSize + 3; L.d("bitmap:111sampleSize =%s ,srcWidth=%s,srcHeight=%s,dstWidth=%s,dstHeight=%s", options.inSampleSize, srcWidth, srcHeight, dstWidth, dstHeight); } options.inJustDecodeBounds = false; return BitmapFactory.decodeFile(path, options); }
第二次缩放,把图片尺寸缩放到指定大小 public Bitmap resizeImage2(Bitmap bitmap, int w, int h) { Bitmap BitmapOrg = bitmap; int width = BitmapOrg.getWidth(); int height = BitmapOrg.getHeight(); int newWidth = w; int newHeight = h; float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; Matrix matrix = new Matrix(); matrix.postScale(scaleWidth, scaleHeight); // if you want to rotate the Bitmap // matrix.postRotate(45); Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0, width, height, matrix, true); return resizedBitmap; }
0 0
- 从路径uri加载Bitmap,缩小图片到指定大小的方法记录
- bitmap 将图片压缩到指定的大小
- [Android算法] bitmap 将图片压缩到指定的大小
- bitmap 将图片压缩到指定的大小 JAVA
- bitmap 将图片压缩到指定的大小
- bitmap 将图片压缩到指定的大小 JAVA
- bitmap 将图片压缩到指定的大小
- [Android算法] bitmap 将图片压缩到指定的大小
- [Android算法] bitmap 将图片压缩到指定的大小
- [Android算法] bitmap 将图片压缩到指定的大小
- Bitmap图片压缩到指定字节大小
- Bitmap缩放到指定的大小
- js技术图片加载并按比例缩小到合适大小
- 图片Uri,从其中获取到图片路径。
- 使用Bitmap保存图片文件到指定路径
- MFC指定图片路径,缩小图片,再保存到一个路径
- Android 通过URI加载网络图片bitmap
- iPhone 将图片缩小成指定大小
- 持续可用与CAP理论 – 一个系统开发者的观点
- Android 设置定时提醒,AlarmManager简单使用
- c语言代码简单陷阱
- 错误集锦
- 同步/异步与阻塞/非阻塞的区别
- 从路径uri加载Bitmap,缩小图片到指定大小的方法记录
- CCF201604-3 路径解析(100分)
- RunLoop优化加载大量图片的卡顿问题
- 第七节 Html字体样式
- restful
- 分布式系统的事务处理经典问题及模型
- Composer更新慢的终极解决方案
- 二、类和对象~方法与函数的区别
- 为Docker容器指定自定义网段的固定IP/静态IP地址