2012-05-27 12 views
10

Estoy construyendo un android donde. Dentro de una actividad tengo un botón de imagen. Cuando hago clic en él, la galería se abre y puedo elegir una imagen. Luego configuro esa imagen como la nueva imagen para el botón de imagen. El problema es que la imagen parece demasiado grande dentro de mi actividad. ¿Cómo puedo hacer que encaje en mi botón de imagen?¿Cómo cambiar el tamaño de una imagen que elegí de la galería en Android?

protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

    switch(requestCode) { 
    case SELECT_PHOTO: 
     if(resultCode == RESULT_OK){ 
      Uri selectedImage = imageReturnedIntent.getData(); 
      String[] filePathColumn = {MediaStore.Images.Media.DATA}; 

      Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null); 
      cursor.moveToFirst(); 

      int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
      String filePath = cursor.getString(columnIndex); 
      cursor.close(); 


      Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath); 

      mImageButton.setImageBitmap(yourSelectedImage); 
     } 
    } 
} 

Respuesta

33

Usted puede usar este método para obtener una imagen redimensionada. De esta manera se puede evitar OutOfMemoryError

public static Bitmap decodeUri(Context c, Uri uri, final int requiredSize) 
      throws FileNotFoundException { 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 
     BitmapFactory.decodeStream(c.getContentResolver().openInputStream(uri), null, o); 

     int width_tmp = o.outWidth 
       , height_tmp = o.outHeight; 
     int scale = 1; 

     while(true) { 
      if(width_tmp/2 < requiredSize || height_tmp/2 < requiredSize) 
       break; 
      width_tmp /= 2; 
      height_tmp /= 2; 
      scale *= 2; 
     } 

     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize = scale; 
     return BitmapFactory.decodeStream(c.getContentResolver().openInputStream(uri), null, o2); 
    } 
+2

¿Puede explicar qué significa el parámetro 'requiredSize'? – TrungDQ

3

Da este LINK

Uso: Bitmap.createScaledBitmap (src mapa de bits, int dstWidth, int dstHeight, filtro booleano)

o utilizar estos métodos ::

public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) { 
    int width = bm.getWidth(); 
    int height = bm.getHeight(); 
    float scaleWidth = ((float) newWidth)/width; 
    float scaleHeight = ((float) newHeight)/height; 

    // create a matrix for the manipulation 
    Matrix matrix = new Matrix(); 

    // resize the bit map 
    matrix.postScale(scaleWidth, scaleHeight); 

    // recreate the new Bitmap 
    Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false); 

    return resizedBitmap; 
} 
+0

he de llamar a ese método después: Bitmap yourSelectedImage = BitmapFactory.decodeFile (rutaArchivo); – user1420042

+0

sí, puede llamar al –

+0

pero luego aparece un error de tiempo de ejecución – user1420042

Cuestiones relacionadas