28
28
import android .renderscript .RenderScript ;
29
29
import android .renderscript .ScriptIntrinsicBlur ;
30
30
31
+ import java .io .BufferedOutputStream ;
31
32
import java .io .ByteArrayInputStream ;
32
33
import java .io .ByteArrayOutputStream ;
33
34
import java .io .File ;
34
35
import java .io .FileInputStream ;
35
- import java .io .FileNotFoundException ;
36
36
import java .io .FileOutputStream ;
37
37
import java .io .IOException ;
38
38
import java .io .InputStream ;
39
+ import java .io .OutputStream ;
39
40
40
41
/**
41
42
* <pre>
@@ -121,32 +122,25 @@ public static Drawable bytes2Drawable(Resources resources, byte[] bytes) {
121
122
/**
122
123
* 根据文件路径获取bitmap
123
124
*
124
- * @param filePath 文件路径
125
+ * @param file 文件路径
125
126
* @return bitmap
126
127
*/
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 ());
129
131
}
130
132
131
133
/**
132
134
* 根据文件路径获取bitmap
133
135
*
134
- * @param file 文件路径
136
+ * @param filePath 文件路径
135
137
* @return bitmap
136
138
*/
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 );
145
141
}
146
142
147
143
/**
148
- * 根据文件路径获取bitmap
149
- *
150
144
* @param filePath 文件路径
151
145
* @return bitmap
152
146
*/
@@ -166,24 +160,19 @@ public static int calculateInSampleSize(
166
160
final int height = options .outHeight ;
167
161
final int width = options .outWidth ;
168
162
int inSampleSize = 1 ;
169
-
170
163
if (height > reqHeight || width > reqWidth ) {
171
-
172
164
final int halfHeight = height / 2 ;
173
165
final int halfWidth = width / 2 ;
174
-
175
166
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
176
167
// height and width larger than the requested height and width.
177
168
while ((halfHeight / inSampleSize ) > reqHeight
178
169
&& (halfWidth / inSampleSize ) > reqWidth ) {
179
- inSampleSize *= 2 ;
170
+ inSampleSize <<= 1 ;
180
171
}
181
172
}
182
-
183
173
return inSampleSize ;
184
174
}
185
175
186
-
187
176
/**
188
177
* 缩放图片
189
178
*
@@ -735,17 +724,17 @@ public static boolean save(Bitmap src, String filePath, CompressFormat format) {
735
724
* @return {@code true}: 成功<br>{@code false}: 失败
736
725
*/
737
726
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 ;
741
730
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 );
744
733
} catch (Exception e ) {
745
734
e .printStackTrace ();
746
735
return false ;
747
736
} finally {
748
- FileUtils .closeIO (fos );
737
+ FileUtils .closeIO (os );
749
738
}
750
739
}
751
740
0 commit comments