yian_ 2015-01-22 15:31 采纳率: 0%
浏览 1787

android 我把图片放入放入缓存。可为什么却还是从网络加载勒?

贴出来代码:
public class MainActivity extends Activity {

    private ImageView ima;
    private MemoryCache lruCache;
       String path="http://android.apkbus.com/images/172034140lit.jpg";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         lruCache=new MemoryCache();
         ima= (ImageView) findViewById(R.id.imageView1);
      //   new ImageTask().execute();
       Bitmap b=   getBit(path);
       ima.setImageBitmap(b);
    }

    class ImageTask extends AsyncTask<String, Void, Bitmap>{
        @Override
        protected Bitmap doInBackground(String... arg0) {
            try {
                URL url=new URL(path);
            try {
                HttpURLConnection conn= (HttpURLConnection) url.openConnection();
                conn.setDoInput(true);
                InputStream in=conn.getInputStream();
                Bitmap bit=BitmapFactory.decodeStream(in);
                return bit;
            } catch (IOException e) {
                e.printStackTrace();
            }

            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
            return null;
        }
        @Override
        protected void onPostExecute(Bitmap result) {
            ima.setImageBitmap(result);
            lruCache.addBitmapToMemoryCache(path, result);
        }
    }

    public Bitmap  getBit(String path){
    Bitmap bb=  lruCache.getBitmapFromMemCache(path);
        if(bb==null){
              new ImageTask().execute();
        }
        return bb;
    }


}
  • 写回答

4条回答 默认 最新

  • yian_ 2015-01-22 15:32
    关注

    补充下。我的图片缓存类没有贴出。 不过是没有问题的。。。

    评论

报告相同问题?