2014-01-06 62 views
0

我的班級應該返回一個默認圖片(在下載圖片或下載失敗時),然後返回下載的圖片(來自url的圖片)。
但它總是返回默認圖像。
如何解決?從我的班級返回一個位圖android

public class ImageLoader { 

MemoryCache memoryCache=new MemoryCache(); 
FileCache fileCache; 
ExecutorService executorService; 
Handler handler=new Handler();//handler to display images in UI thread 
Context context; 
public Bitmap returnbitmap; 

public ImageLoader(Context context){ 
    this.context=context; 
    fileCache=new FileCache(context); 
    executorService=Executors.newFixedThreadPool(5); 
} 
final int stub_id=R.drawable.stub; 

public Bitmap DisplayBitmap(String url) 
{ 
    Bitmap bitmap=memoryCache.get(url); 
    if(bitmap!=null) 
     return bitmap; 
    else 
    { 
     queueBitmap(url); 
     return BitmapFactory.decodeResource(context.getResources(), stub_id); 
    } 
} 

private void queueBitmap(String url) 
{ 
    BitmapToLoad p=new BitmapToLoad(url); 
    executorService.submit(new BitmapsLoader(p)); 
} 

private Bitmap getBitmap(String url) 
{ 
    File f=fileCache.getFile(url); 

    //from SD cache 
    Bitmap b = decodeFile(f); 
    if(b!=null) 
     return b; 

    //from web 
    try { 
     Bitmap bitmap=null; 
     URL imageUrl = new URL(url); 
     HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection(); 
     conn.setConnectTimeout(30000); 
     conn.setReadTimeout(30000); 
     conn.setInstanceFollowRedirects(true); 
     InputStream is=conn.getInputStream(); 
     OutputStream os = new FileOutputStream(f); 
     Utils.CopyStream(is, os); 
     os.close(); 
     conn.disconnect(); 
     bitmap = decodeFile(f); 
     return bitmap; 
    } catch (Throwable ex){ 
     ex.printStackTrace(); 
     if(ex instanceof OutOfMemoryError) 
      memoryCache.clear(); 
     return null; 
    } 
} 

//decodes image and scales it to reduce memory consumption 
private Bitmap decodeFile(File f){ 
    try { 
     //decode image size 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 
     FileInputStream stream1=new FileInputStream(f); 
     BitmapFactory.decodeStream(stream1,null,o); 
     stream1.close(); 

     //Find the correct scale value. It should be the power of 2. 
     final int REQUIRED_SIZE=70; 
     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; 
     } 

     //decode with inSampleSize 
     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize=scale; 
     FileInputStream stream2=new FileInputStream(f); 
     Bitmap bitmap=BitmapFactory.decodeStream(stream2, null, o2); 
     stream2.close(); 
     return bitmap; 
    } catch (FileNotFoundException e) { 
    } 
    catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 

private class BitmapToLoad 
{ 
    public String url; 
    public BitmapToLoad(String u){ 
     url=u; 
    } 
} 

class BitmapsLoader implements Runnable { 
    BitmapToLoad bitmapToLoad; 
    BitmapsLoader(BitmapToLoad bitmapToLoad){ 
     this.bitmapToLoad=bitmapToLoad; 
    } 

    @Override 
    public void run() { 
     try{ 
      Bitmap bmp=getBitmap(bitmapToLoad.url); 
      memoryCache.put(bitmapToLoad.url, bmp); 
      BitmapDisplay bd=new BitmapDisplay(bmp, bitmapToLoad); 
      handler.post(bd); 
     }catch(Throwable th){ 
      th.printStackTrace(); 
     } 
    } 
} 

class BitmapDisplay implements Runnable 
{ 
    Bitmap bitmap; 
    BitmapToLoad bitmapToLoad; 
    public BitmapDisplay(Bitmap b, BitmapToLoad p){bitmap=b;bitmapToLoad=p;} 
    public void run() 
    { 
     if(bitmap!=null) 
     returnbitmap=bitmap; 
     else 
      returnbitmap=BitmapFactory.decodeResource(context.getResources(), stub_id); 
    } 
} 

} 

並使用我的類imageLoader.DisplayBitmap(「URL image」);
如何解決它?

回答

0

充分利用DisplayBitmap方法靜態如

公共靜態DisplayBitmap(的MemoryCache緩存,字符串URL);

+0

memoryCache,queueBitmap,context,stub_id錯誤無法從類型ImageLoader對非靜態方法queueBitmap(String)進行靜態引用,這不解決嗎? – user2790938

+0

將您的方法更改爲靜態方法 –

+0

當然,所有成員變量都必須靜態定義。 – allsoft