2828import android .renderscript .RenderScript ;
2929import android .renderscript .ScriptIntrinsicBlur ;
3030
31+ import java .io .BufferedOutputStream ;
3132import java .io .ByteArrayInputStream ;
3233import java .io .ByteArrayOutputStream ;
3334import java .io .File ;
3435import java .io .FileInputStream ;
35- import java .io .FileNotFoundException ;
3636import java .io .FileOutputStream ;
3737import java .io .IOException ;
3838import java .io .InputStream ;
39+ import java .io .OutputStream ;
3940
4041/**
4142 * <pre>
@@ -121,32 +122,25 @@ public static Drawable bytes2Drawable(Resources resources, byte[] bytes) {
121122 /**
122123 * 根据文件路径获取bitmap
123124 *
124- * @param filePath 文件路径
125+ * @param file 文件路径
125126 * @return bitmap
126127 */
127- public static Bitmap getBitmapByFile (String filePath ) {
128- return getBitmapByFile (FileUtils .getFileByPath (filePath ));
128+ public static Bitmap getBitmapByFile (File file ) {
129+ if (file == null ) return null ;
130+ return getBitmapByFile (file .getPath ());
129131 }
130132
131133 /**
132134 * 根据文件路径获取bitmap
133135 *
134- * @param file 文件路径
136+ * @param filePath 文件路径
135137 * @return bitmap
136138 */
137- public static Bitmap getBitmapByFile (File file ) {
138- if (file == null ) return null ;
139- try {
140- return BitmapFactory .decodeStream (new FileInputStream (file ));
141- } catch (FileNotFoundException e ) {
142- e .printStackTrace ();
143- return null ;
144- }
139+ public static Bitmap getBitmapByFile (String filePath ) {
140+ return BitmapFactory .decodeFile (filePath );
145141 }
146142
147143 /**
148- * 根据文件路径获取bitmap
149- *
150144 * @param filePath 文件路径
151145 * @return bitmap
152146 */
@@ -166,24 +160,19 @@ public static int calculateInSampleSize(
166160 final int height = options .outHeight ;
167161 final int width = options .outWidth ;
168162 int inSampleSize = 1 ;
169-
170163 if (height > reqHeight || width > reqWidth ) {
171-
172164 final int halfHeight = height / 2 ;
173165 final int halfWidth = width / 2 ;
174-
175166 // Calculate the largest inSampleSize value that is a power of 2 and keeps both
176167 // height and width larger than the requested height and width.
177168 while ((halfHeight / inSampleSize ) > reqHeight
178169 && (halfWidth / inSampleSize ) > reqWidth ) {
179- inSampleSize *= 2 ;
170+ inSampleSize <<= 1 ;
180171 }
181172 }
182-
183173 return inSampleSize ;
184174 }
185175
186-
187176 /**
188177 * 缩放图片
189178 *
@@ -735,17 +724,17 @@ public static boolean save(Bitmap src, String filePath, CompressFormat format) {
735724 * @return {@code true}: 成功<br>{@code false}: 失败
736725 */
737726 public static boolean save (Bitmap src , File file , CompressFormat format ) {
738- if (isEmptyBitmap (src ) || file == null ) return false ;
739- System .out .println (src .getWidth () + "," + src .getHeight ());
740- FileOutputStream fos = null ;
727+ if (isEmptyBitmap (src ) || ! FileUtils . createOrExistsFile ( file ) ) return false ;
728+ System .out .println (src .getWidth () + ", " + src .getHeight ());
729+ OutputStream os = null ;
741730 try {
742- fos = new FileOutputStream (file );
743- return src .compress (format , 100 , fos );
731+ os = new BufferedOutputStream ( new FileOutputStream (file ) );
732+ return src .compress (format , 100 , os );
744733 } catch (Exception e ) {
745734 e .printStackTrace ();
746735 return false ;
747736 } finally {
748- FileUtils .closeIO (fos );
737+ FileUtils .closeIO (os );
749738 }
750739 }
751740
0 commit comments