rt  如何把一个图片处理成固定像素大小的另外一个图片并保存下来处理后的  而原文件不动呢?如  在D位置有个1024*768的图片  怎么处理成480*800的图片存在E位置呢?D位置的图片不动 谢谢!

解决方案 »

  1.   

    大概是这样子的
    //判断本地文件是否存在   
    File fileS = new File(Sfilename);
    if(fileS.exists())
    {   //本地文件存在    
                        BitmapFactory.Options options = new BitmapFactory.Options();
                        options.inJustDecodeBounds = true;                     // 获取这个图片原始的宽和高 在outHeight 及 outWidth
                        Bitmap bm = BitmapFactory.decodeFile(fileS.getPath(), options); //此时返回bm为空
                        //  我们要得到高及宽都不超过W H的缩略图
                        int zW = options.outWidth/800;
                        int zH = options.outHeight/480;
                        int be = zH;
                        if(zW > be)
                            be = zW;
                        
                        if(be == 0) be = 1;
                        
                        options.inSampleSize = be;
                        options.inJustDecodeBounds = false;  
                        bm = BitmapFactory.decodeFile(fileS.getPath(), options);                    try {
                            File fileD = new File(Dfilename);
                            if(!fileD.exists())
                            { //本地文件不存在,则保存
                                myConfig.SaveBitmap(result,Dfilename);
                            }
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            myConfig.LogPrintf("ImageDownloadTask.onPostExecute() err:"+e.getMessage());
                        }
                    }
      

  2.   

    /** 
     * @将图片对象保存为PNG格式文件
     * 
     */
    public void SaveBitmap(Bitmap bitmap,String filename) throws IOException
        {
    File file = new File(ImageDir, filename);
           FileOutputStream out;
           try{
           out = new FileOutputStream(file);
           if(bitmap.compress(Bitmap.CompressFormat.PNG, 70, out))
             {
           out.flush();
                 out.close();
              }
          }
           catch (FileNotFoundException e)
           {
           LogPrintf("SaveBitmap err:"+e.getMessage());
           }
           catch (IOException e)
           {
           LogPrintf("SaveBitmap err:"+e.getMessage());
           }
        }