从android中的类中返回一个位图

时间:2014-01-06 06:43:40

标签: android bitmap return

我的班级应返回默认图片(下载图片或下载失败时),然后返回下载的图片(图片来自网址)。
但它总是返回默认图像 怎么解决?

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”);
如何解决?

1 个答案:

答案 0 :(得分:0)

静态制作DisplayBitmap方法,例如

public static DisplayBitmap(MemoryCache cache,String url);