国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

從路徑uri加載Bitmap,縮小圖片到指定大小的方法記錄

2019-11-09 15:09:10
字體:
供稿:網(wǎng)友
根據(jù)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 ""; }加載時第一次縮放主要是為了縮小圖片,節(jié)約加載內(nèi)存 public Bitmap resizeImage1(String path,int dstWidth, int dstHeight) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true;//不加載bitmap到內(nèi)存中 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的指數(shù)冪時才有效如1,2,4,6,這里寧愿多壓縮一點節(jié)約內(nèi)存所以加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; }
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 金沙县| 望奎县| 葫芦岛市| 炎陵县| 肥西县| 宁明县| 仁布县| 江永县| 甘肃省| 中方县| 漳浦县| 永胜县| 台南市| 大丰市| 马鞍山市| 眉山市| 保定市| 夏津县| 资源县| 荆门市| 华容县| 临沭县| 沧州市| 清涧县| 灵石县| 庆云县| 孝感市| 南丹县| 钦州市| 汶川县| 灵武市| 科尔| 依兰县| 宜阳县| 长沙市| 桦甸市| 宜黄县| 会昌县| 清镇市| 秦皇岛市| 平和县|