2012-05-27 25 views
10

Tôi đang xây dựng một android ở đâu. Bên trong một hoạt động tôi có một nút hình ảnh. Khi tôi nhấp vào nó, thư viện mở ra và tôi có thể chọn một hình ảnh. Sau đó, tôi đặt hình ảnh đó làm hình ảnh mới cho nút hình ảnh. Vấn đề là hình ảnh xuất hiện quá lớn trong hoạt động của tôi. Làm thế nào tôi có thể làm cho nó phù hợp với nút hình ảnh của tôi?Làm cách nào để thay đổi kích cỡ hình ảnh tôi đã chọn từ thư viện trong 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); 
     } 
    } 
} 

Trả lời

33

Bạn có thể sử dụng phương pháp này để có được một hình ảnh thay đổi kích cỡ. Bằng cách này bạn có thể tránh 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

Bạn có thể giải thích tham số 'requiredSize' có nghĩa là gì không? – TrungDQ

3

Tham khảo LINK

này sử dụng: Bitmap.createScaledBitmap (Bitmap src, int dstWidth, int dstHeight, boolean lọc)

hoặc sử dụng các phương pháp ::

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

tôi sẽ gọi đó là phương pháp sau: Bitmap yourSelectedImage = BitmapFactory.decodeFile (filepath); – user1420042

+0

có bạn có thể gọi –

+0

nhưng sau đó tôi nhận được lỗi thời gian chạy – user1420042

Các vấn đề liên quan