Android 开发图片压缩/缩略图的方法

FrameLayout fr=(FrameLayout)findViewById(R.id.FrameLayout01);
        BitmapFactory.Options options =new BitmapFactory.Options();
        options.inJustDecodeBounds =true;
        // 获取这个图片的宽和高
        Bitmap bitmap =BitmapFactory.decodeFile("/sdcard/test.jpg", options); //此时返回bm为空
        options.inJustDecodeBounds =false;
         //计算缩放比
        int be = (int)(options.outHeight/ (float)200);
        if (be <= 0)
            be = 1;
        options.inSampleSize = be;
        //重新读入图片,注意这次要把options.inJustDecodeBounds 设为 false哦
       bitmap=BitmapFactory.decodeFile("/sdcard/test.jpg",options);
        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        System.out.println(w+"   "+h);
        ImageView iv=new ImageView(this);
        iv.setImageBitmap(bitmap);


File file=new File("/sdcard/feng.png");
        try {
            FileOutputStream out=newFileOutputStream(file);
           if(bitmap.compress(Bitmap.CompressFormat.PNG, 100, out)){
                out.flush();
                out.close();
            }
        } catch (FileNotFoundException e){
            // TODO Auto-generated catchblock
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catchblock
            e.printStackTrace();
        }

你可能感兴趣的:(android,开发图片压缩/缩略图的方法)