【问题标题】:Return a bitmap from my class in android从我在android中的班级返回位图
【发布时间】:2014-01-06 06:43:39
【问题描述】:

我的课程应该返回一个默认图像(在下载图像或下载失败时),然后返回下载的图像(来自 url 的图像)。
但它总是返回默认图像。
如何解决?

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

【问题讨论】:

    标签: android bitmap return


    【解决方案1】:

    静态制作DisplayBitmap方法如

    public static DisplayBitmap(MemoryCache cache, String url);

    【讨论】:

    • error in memoryCache , queueBitmap , context, stub_id 无法从 ImageLoader 类型对非静态方法 queueBitmap(String) 进行静态引用,这不能解决吗?!
    • 将您的方法更改为静态方法
    • 当然,所有成员变量都必须静态定义。
    猜你喜欢
    • 1970-01-01
    • 2012-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多