1.通过存放图片的路径,压缩图片,使用方法compress()
public static Bitmap getSmallBitmap(String filePath) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options,240,240);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
Bitmap bm = BitmapFactory.decodeFile(filePath, options);
if(bm == null){
return null;
}
ByteArrayOutputStream baos = null ;
try{
baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG,50, baos);
}finally{
try {
if(baos != null)
baos.close() ;
} catch (IOException e) {
e.printStackTrace();
}
}
return bm ;
}
private static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
// Calculate ratios of height and width to requested height and
// width
final int heightRatio = Math.round((float) height
/ (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will
// guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? widthRatio : heightRatio;
}
return inSampleSize;
}
2.把图片保存到指定的路径
/***
* 把bitmap保存到指定路径
*/
private void saveCroppedImage(Bitmap bmp,String imgName) {
File file = new File("/sdcard/myFolder");
if (!file.exists())
file.mkdir();
// file = new File("/sdcard/temp.jpg".trim());
file = new File("/sdcard/"+imgName.trim());///sdcard/temp.jpg //这里imgName 替代 temp.jpg
urls="/sdcard/myFolder";
String fileName = file.getName();//temp.jpg
String mName = fileName.substring(0, fileName.lastIndexOf("."));//temp
String sName = fileName.substring(fileName.lastIndexOf("."));//.jpg
// /sdcard/myFolder/temp_cropped.jpg
String newFilePath = "/sdcard/myFolder" + "/" + mName + "_c" + sName;// /sdcard/myFolder/temp_cropped.jpg
file = new File(newFilePath);// /sdcard/myFolder/temp_cropped.jpg
try {
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
bmp.compress(CompressFormat.JPEG, 50, fos);
fos.flush();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
3.
/**
* 从指定路径取出图片的路径集合
* 获取SDCard中某个目录下图片路径集合
* @param strPath
* @return
*/
public List<String> getPictures(final String strPath) {
List<String> list = new ArrayList<String>();
File file = new File(strPath);
File[] allfiles = file.listFiles();
if (allfiles == null) {
return null;
}
for(int k = 0; k < allfiles.length; k++) {
final File fi = allfiles[k];
if(fi.isFile()) {
int idx = fi.getPath().lastIndexOf(".");
if (idx <= 0) {
continue;
}
String suffix = fi.getPath().substring(idx);
if (suffix.toLowerCase().equals(".jpg") ||
suffix.toLowerCase().equals(".jpeg") ||
suffix.toLowerCase().equals(".bmp") ||
suffix.toLowerCase().equals(".png") ||
suffix.toLowerCase().equals(".gif") ) {
list.add(fi.getPath());
}
}
}
return list;
}