2013-05-04 60 views
0

我有一个ImageButton,当我按下它时,我可以用手机拍照。当我拍摄照片时,ImageButtons将图片作为其来源。但问题是,图像是不是缩小到ImageButton的尺寸。它只是在Imagebutton中显示图像的一部分,而不是缩小。在做了一些研究之后,我看到你必须使用一个平局9补丁。但在这种情况下这是不可能的,因为图像不在我的资源中,它们是由用户自己制作的。刚刚拍下照片到imagebutton

有人可以帮助我吗?

我的代码:

if (requestCode == REQUEST_CAMERA) { 
    File f = new File(Environment.getExternalStorageDirectory().toString()); 
    for (File temp : f.listFiles()) { 
     if (temp.getName().equals("temp.jpg")) { 
      f = temp; 
      break; 
     } 
    } 

    try { 
     Bitmap bm; 
     BitmapFactory.Options btmapOptions = new BitmapFactory.Options(); 
     bm = BitmapFactory.decodeFile(f.getAbsolutePath(),btmapOptions); 
     // bm = Bitmap.createScaledBitmap(bm, 70, 70, true); 
     btnImg.setImageBitmap(bm); 
     String path = android.os.Environment.getExternalStorageDirectory() + File.separator + "Phoenix" + File.separator + "default"; 
     f.delete(); 
     OutputStream fOut = null; 
     File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg"); 

     try { 
       fOut = new FileOutputStream(file); 
      bm.compress(Bitmap.CompressFormat.JPEG, 85, fOut); 
      fOut.flush(); 
      fOut.close(); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } catch (Exception e) { 
      e.printStackTrace(); 
     } 
      } 
} 
else if (requestCode == SELECT_FILE) { 
Uri selectedImageUri = data.getData(); 
String tempPath = getPath(selectedImageUri, MainActivity.this); 
Bitmap bm; 
BitmapFactory.Options btmapOptions = new BitmapFactory.Options(); 
bm = BitmapFactory.decodeFile(tempPath, btmapOptions); 
btnImg.setImageBitmap(bm); 
} 

回答

0

使用的ImageView方法setScaleType。通过ScaleType.CENTER_INSIDE作为参数。

btnImg.setImageBitmap(bm); 
btnImg.setScaleType(ScaleType.CENTER_INSIDE); 
+0

这工作对我来说,但现在,插入第二个图像后,应用程序崩溃..这是相关的,还是我应该提出一个新的问题呢? – mXX 2013-05-04 19:34:54

+0

必须是OutOfMemoryException。添加一个新问题。 – Ronnie 2013-05-04 19:37:31