2014-01-07 22 views
0

嘿大家,这是代码,通过它我们可以调整图像尺寸,但有一个在第一和第二线,我解决不了这个,请帮助我图像的大小调整代码错误

Bitmap image2 = (Bitmap) data.getExtras().get("data"); 
     imageView.setImageBitmap(image2); 
     /*String incident_ID = IncidentFormActivity.incident_id;*/ 

     String imagepath = "\1.jpg"; 
     File file = new File(imagepath); 

       double xFactor = 0; 
       double width = Double.valueOf(image2.getWidth()); 
       Log.v("WIDTH", String.valueOf(width)); 
       double height = Double.valueOf(image2.getHeight()); 
       Log.v("height", String.valueOf(height)); 



     Log.v("Nheight", String.valueOf(width*xFactor)); 
     Log.v("Nweight", String.valueOf(height*xFactor)); 
     int Nheight = 480; 
     int NWidth = (int) ((Nheight * width)/height); 

     Bitmap bm = Bitmap.createScaledBitmap(image2,NWidth, Nheight, true); 
     file.createNewFile(); 
     FileOutputStream ostream = new FileOutputStream(file); 
     bm.compress(CompressFormat.PNG, 100, ostream); 
     ostream.close(); 
+0

错误发生在这些行中 – user3167821

+0

位图image2 =(位图)data.getExtras()。get(“data”); imageView.setImageBitmap(图像2); – user3167821

+0

请发布错误.. – saa

回答

0

使用此功能。它的工作非常适合我

static public Bitmap getResizedBitmap(Bitmap image, int maxSize) { 
     int width = image.getWidth(); 
     int height = image.getHeight(); 
     float bitmapRatio = width/height; 
     if (bitmapRatio > 0) { 
      width = maxSize; 
      height = (int) (width/bitmapRatio); 
     } else { 
      height = maxSize; 
      width = (int) (height * bitmapRatio); 
     } 
     return Bitmap.createScaledBitmap(image, width, height, true); 
} 

这里最大尺寸指的是像素,直到图像将被redudes

例如,如果你想减少至640像素 简单地调用`

Bitmap image2 = (Bitmap) data.getExtras().get("data"); 
getResizedBitmap(image2 , 640);