2013-01-05 2 views
0

저는 android를 처음 사용합니다. 나는 이미지 리스팅 양식을 sd - card하고있다. 다음 코드는 "614416 바이트 할당 메모리 부족."오류를 제공합니다.메모리 부족 예외

public View getView(int position, View convertView, ViewGroup parent) { 
     LayoutInflater layoutInflater = (LayoutInflater) ctx 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = layoutInflater.inflate(R.layout.ll_sponsor_list_item, 
       parent, false); 

     final ImageView img = (ImageView) convertView 
       .findViewById(R.id.imggrid_item_image); 
     String imgurl = ImageName.get(position); 

     AsyncImageLoaderv asyncImageLoaderv = new AsyncImageLoaderv(); 
     Bitmap cachedImage = asyncImageLoaderv.loadDrawable(imgurl, 
       new AsyncImageLoaderv.ImageCallback() { 
        public void imageLoaded(Bitmap imageDrawable, 
          String imageUrl) { 
         img.setImageBitmap(imageDrawable); 


        } 
       }); 
     img.setImageBitmap(cachedImage); 
     cachedImage.recycle(); 
     return convertView; 
    } 

등급 :

class AsyncImageLoaderv { 
    int width; 
    int height; 
    float aspectRatio; 
    int newWidth; 
    int newHeight; 

    public Bitmap loadDrawable(final String imageUrl, 
      final ImageCallback imageCallback) { 
     final Handler handler = new Handler() { 
      @Override 
      public void handleMessage(Message message) { 
       imageCallback.imageLoaded((Bitmap) message.obj, imageUrl); 
      } 
     }; 
     new Thread() { 
      @Override 
      public void run() { 
       try { 
        Log.d("ur", imageUrl); 
        Bitmap drawable = BitmapFactory.decodeFile(imageUrl); 
        width = drawable.getWidth(); 
        height = drawable.getWidth(); 
        aspectRatio = (float) width/(float) height; 
        newWidth = 98; 
        newHeight = (int) (98/aspectRatio); 
        Bitmap.createScaledBitmap(drawable, newWidth, newHeight, 
          true); 
        Message message = handler.obtainMessage(0, drawable); 
        handler.sendMessage(message); 
        //this.sleep(1000); 

       } catch (Exception e) { 
        Log.e("thread stellent", e.toString()); 
       } 
      } 
     }.start(); 
     return null; 
    } 

    public interface ImageCallback { 
     public void imageLoaded(Bitmap imageBitmap, String imageUrl); 
    } 
} 
+0

이미지가 무거움인지 확인하십시오 ...이 오류는 무거운 이미지를로드하려고 시도하고 있음을 나타냅니다. – AndroidLearner

+0

이미지를 메모리로로드 한 다음 크기를 조정 중입니다. 'inSampleSize'를 사용하여 크기를 조정해야합니다. 이 문제를 해결하는 방법을 보여주는 많은 중복 된 질문이 있습니다. 그리고 안드로이드 문서. – Doomsknight

답변