2013-01-05 212 views
0

我是android新手。我正在做圖像清單形式的SD卡。以下代碼給我錯誤「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'來縮放它。有很多重複的問題顯示如何解決這個問題。和android文檔。 – Doomsknight

回答