public static String get450File(String mStrPicPath) { int angel = UtilImage.readPictureDegree(mStrPicPath); BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; Bitmap bmp = BitmapFactory.decodeFile(mStrPicPath, options); bmp = null; options.inJustDecodeBounds = false; options.inPreferredConfig = Bitmap.Config.RGB_565; options.inPurgeable = true; options.inInputShareable = true; int scale = 1; if (options.outWidth > UtilFile.BITMAP_WIDTH_SHOW) scale = (int) (options.outWidth / UtilFile.BITMAP_WIDTH_SHOW); options.inSampleSize = scale; bmp = BitmapFactory.decodeFile(mStrPicPath, options); /** 把图片旋转为正的方向 */ bmp = UtilImage.rotaingImageView(angel, bmp); String str_dir = UtilFile.getFolderPath(UtilFile.DIR_PHOTO_CACHE); File dir = new File(str_dir); File filepic = new File(dir, System.currentTimeMillis() + ".jpg"); try { bmp.compress(CompressFormat.JPEG, 100, new FileOutputStream(filepic)); } catch (FileNotFoundException e) { e.printStackTrace(); } // 回收bitmap recycleBitmap(bmp); if (filepic != null) return filepic.toString(); return ""; }
public static String callCamera(Fragment activity) { String imagePath = null; String str_dir = UtilFile.getFolderPath(UtilFile.DIR_PHOTO_CACHE); if (str_dir == null || "".equals(str_dir)) { return imagePath; } File dir = new File(str_dir); File filepic = new File(dir, System.currentTimeMillis() + ".png"); imagePath = filepic.getAbsolutePath(); // 缓存图片路径 callCamera(activity, imagePath); return imagePath; }
/** * 根据从本地获取衣服图片的uri来保存图片 返回 路径 * * @param uri * @param context * @return 注意判断返回值为null */ public static String saveImageForLocal(Uri uri, Context context) { String imagePath = null; ContentResolver cr = context.getContentResolver(); FileOutputStream os = null; InputStream is = null; File file_pic = null; try { // 下面就是保存图片到缓存目录下面 is = cr.openInputStream(uri); String str_dir = UtilFile.getFolderPath(UtilFile.DIR_CLOTH); if (str_dir == null || "".equals(str_dir)) { return imagePath; } String str_pic = System.currentTimeMillis() + ".png"; File dir = new File(str_dir); file_pic = new File(dir, str_pic); os = new FileOutputStream(file_pic); byte[] data = new byte[1024]; int len; while ((len = is.read(data, 0, 1024)) > 0) { os.write(data, 0, len); } os.flush(); imagePath = file_pic.getAbsolutePath(); } catch (Exception e) { if (file_pic != null) { file_pic.deleteOnExit(); } e.printStackTrace(); } finally { try { if (os != null) { os.close(); } if (is != null) { is.close(); } } catch (IOException e) { e.printStackTrace(); } } return imagePath; }
/** * 裁剪图片 主页背景图 * * @param context * @return 注意判断返回值为null */ public static Intent getImageClipIntentForBg( Context context, int scalex, int scaley, int outx, int outy) { String dir = UtilFile.getFolderPath(UtilFile.DIR_PORTRAIT); if (dir == null || "".equals(dir)) { return null; } Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null); intent.setType("image/*"); intent.putExtra("crop", "true"); // Trim Scale intent.putExtra("aspectX", scalex); intent.putExtra("aspectY", scaley); // Image Size intent.putExtra("outputX", outx); intent.putExtra("outputY", outy); // intent.putExtra("return-data", true); intent.putExtra("outputFormat", "JPEG"); intent.putExtra("output", Uri.fromFile(new File(dir + UtilFile.PORTRAIT))); return intent; }
/** * 从缓存以及本地获得图片 * * @param url * @param dir * @return */ private static Bitmap getBitmap(String url, String dir, int bmp_size) { Bitmap bmp = null; boolean isNext = true; if (url != null && !url.equals("")) { String fileName = url.substring(url.lastIndexOf("/") + 1); // 1、从缓存看bitmap是否存在 SoftReference<Bitmap> softReference = null; switch (bmp_size) { case BMP_160: softReference = imageCache_bmp_160.get(fileName); break; case BMP_450: softReference = imageCache_bmp.get(fileName); break; } if (softReference != null) { bmp = softReference.get(); if (bmp != null && !bmp.isRecycled()) { addCache(fileName, null, bmp, bmp_size); isNext = false; } else { imageCache_bmp.remove(fileName); } } // 2、从缓存中看文件路径是否存在 if (isNext) { String filePath = null; switch (bmp_size) { case BMP_160: filePath = imageCache_file_160.get(fileName); break; case BMP_450: filePath = imageCache_file.get(fileName); break; } if (filePath != null && !"".equals(filePath)) { if (bmp_size == BMP_450 && dir.equals(UtilFile.DIR_CACHE)) { // 得到450的我的衣橱图片,因为原图都很大 // 需要缩放 BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; bmp = BitmapFactory.decodeFile(filePath, options); bmp = null; options.inJustDecodeBounds = false; options.inPreferredConfig = Bitmap.Config.RGB_565; options.inPurgeable = true; options.inInputShareable = true; int scale = (int) (options.outWidth / UtilFile.BITMAP_WIDTH_SHOW); options.inSampleSize = scale; bmp = BitmapFactory.decodeFile(filePath, options); } else { bmp = BitmapFactory.decodeFile(filePath); } if (bmp != null) { addCache(fileName, filePath, bmp, bmp_size); isNext = false; } } else { imageCache_file.remove(fileName); } } // 3、判断文件名是否存在 if (isNext) { String filePath = null; switch (bmp_size) { case BMP_160: filePath = UtilFile.getFolderPath(dir) + "/" + fileName + UtilFile.IMG_BAK; break; case BMP_450: filePath = UtilFile.getFolderPath(dir) + "/" + fileName; break; } if (UtilFile.isFileExist(filePath)) { if (bmp_size == BMP_450 && dir.equals(UtilFile.DIR_CACHE)) { // 得到450的我的衣橱图片,因为原图都很大 // 需要缩放 BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; bmp = BitmapFactory.decodeFile(filePath, options); bmp = null; options.inJustDecodeBounds = false; options.inPreferredConfig = Bitmap.Config.RGB_565; options.inPurgeable = true; options.inInputShareable = true; int scale = (int) (options.outWidth / UtilFile.BITMAP_WIDTH_SHOW); options.inSampleSize = scale; bmp = BitmapFactory.decodeFile(filePath, options); } else { bmp = BitmapFactory.decodeFile(filePath); } if (bmp != null) { addCache(fileName, filePath, bmp, bmp_size); isNext = false; } else { UtilFile.deleteFile(filePath); } } } } return bmp; }