2013-10-17 2 views
1

내 Android 애플리케이션에서 사용자가 갤러리를 열고 이미지를 선택하기위한 버튼을 클릭 할 수있게해야합니다. 그런 다음 레이아웃 (UI)의 특정 이미지를 이미지 뷰에로드해야합니다. 그리고 몇 가지 코드가 있지만 그것은 java.lang.outofmemory.Please 사람이 나를 도울 수 온다? 여기Android : 이미지를 업로드하는 동안 기기에서 앱이 다운 됨, java.lang.outofMemoryError

+0

로드하는 비트 맵이 테스트 장치에서 사용 가능한 메모리 양에 비해 너무 클 수 있습니다. 또는 한 번에 너무 많은 비트 맵을 갤러리에로드 할 수 있습니다. 사람들은 종종 이것을 실행하기 때문에 Android에서 비트 맵을 처리 할 때는 코드에 미리 고려해야합니다. 다음은 Romain Guy가이 주제를 다루는 프레젠테이션입니다. https://dl.google.com/io/2009/pres/Th_0230_TurboChargeYourUI-HowtomakeyourAndroidUIfastandefficient.pdf – Turnsole

+0

@Turnsole 작동에 많은 감사를드립니다. – shakthivel

답변

3

당신은 onActivityResult를() 메소드의 이미지 URI를 디코딩한다. 이 메소드를 호출하여 decodeBitmap을 디코딩하십시오. 주제 이 효율적으로

http://developer.android.com/training/displaying-bitmaps/index.html

희망이 도움말을 비트 맵을 표시하지만 자세한 내용은

/** 
    * This is very useful to overcome Memory waring issue while selecting image 
    * from Gallery 
    * 
    * @param selectedImage 
    * @param context 
    * @return Bitmap 
    * @throws FileNotFoundException 
    */ 
    public static Bitmap decodeBitmap(Uri selectedImage, Context context) 
      throws FileNotFoundException { 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 
     BitmapFactory.decodeStream(context.getContentResolver() 
       .openInputStream(selectedImage), null, o); 

     final int REQUIRED_SIZE = 100; 

     int width_tmp = o.outWidth, height_tmp = o.outHeight; 
     int scale = 1; 
     while (true) { 
      if (width_tmp/2 < REQUIRED_SIZE || height_tmp/2 < REQUIRED_SIZE) { 
       break; 
      } 
      width_tmp /= 2; 
      height_tmp /= 2; 
      scale *= 2; 
     } 

     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize = scale; 
     return BitmapFactory.decodeStream(context.getContentResolver() 
       .openInputStream(selectedImage), null, o2); 
    } 

는 간다.

+0

고맙습니다. 벌금.. – shakthivel

관련 문제