将文件中的图片拿出来,设置为固定大小并保存
方法一:
2.方法二
//-------------------保存缩略
方法一:
public Bitmap convertToBitmap(String path, int w, int h) {
BitmapFactory.Options opts = new BitmapFactory.Options();
// 设置为ture只获取图片大小
opts.inJustDecodeBounds = true;
opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
// 返回为空
BitmapFactory.decodeFile(path, opts);
int width = opts.outWidth;
int height = opts.outHeight;
float scaleWidth = 0.f, scaleHeight = 0.f;
if (width > w || height > h) {
// 缩放
scaleWidth = ((float) width) / w;
scaleHeight = ((float) height) / h;
}
opts.inJustDecodeBounds = false;
float scale = Math.max(scaleWidth, scaleHeight);
opts.inSampleSize = (int) scale;
WeakReference<Bitmap> weak = new WeakReference<Bitmap>(
BitmapFactory.decodeFile(path, opts));
return Bitmap.createScaledBitmap(weak.get(), w, h, true);
}
2.方法二
//-------------------保存缩略
Bitmap bitmap =BitmapFactory.decodeFile(saveFilePate);
saveFile(zoomImage(bitmap, 200, 150), savePath ,filename);
// 产生缩略图
private Bitmap zoomImage(Bitmap bgimage, int newWidth, int newHeight) {
int width = bgimage.getWidth();
int height = bgimage.getHeight();
Matrix matrix = new Matrix();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
matrix.postScale(scaleWidth, scaleHeight);
Bitmap bitmap = Bitmap.createBitmap(bgimage, 0, 0, width, height,
matrix, true);
return bitmap;
}
//保存方法
private void saveFile(Bitmap bm, String path, String fileName)
throws IOException {
File dirFile = new File(path);
if (!dirFile.exists()) {
dirFile.mkdir();
}
File myCaptureFile = new File(path + "/" + fileName);
if (myCaptureFile.exists()) {
myCaptureFile.delete();
}
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(myCaptureFile));
bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
bos.flush();}