android 按比例获取SD卡缩略图

                 BitmapFactory.Options options = new BitmapFactory.Options();  
options.inJustDecodeBounds = true;

//获取这个图片的宽和高
Bitmap bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getAbsolutePath()+ "/darunfa/temp.jpg",options);//此时返回bm为空
options.inJustDecodeBounds =false;
//计算缩放比
int be = (int)(options.outWidth / (float)600);
if(be <= 0)
be = 1;
options.inSampleSize = be;
//重新读入图片,注意这次要把options.inJustDecodeBounds设为false哦
bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getAbsolutePath()+ "/darunfa/temp.jpg",options);
int w = bitmap.getWidth();
int h = bitmap.getHeight();
System.out.println(w+" "+h);
imageView.setImageBitmap(bitmap);

File file2= new File(Environment.getExternalStorageDirectory().getAbsolutePath()+ "/darunfa/aaaa.jpg");
try {
FileOutputStream out = new FileOutputStream(file2);
if(bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out)){
out.flush();
out.close();
}
} catch (Exception e) {
// TODO: handle exception
}
posted @ 2012-03-28 15:31  fighter  阅读(1299)  评论(0编辑  收藏  举报