第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定

我的程序有一個問題,就是在添加了LruCache后就不顯示圖片了

package com.example.imoc.Adapter;

import java.io.BufferedInputStream;

import java.io.IOException;

import java.io.InputStream;

import java.net.HttpURLConnection;

import java.net.MalformedURLException;

import java.net.URL;

import android.annotation.SuppressLint;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.os.AsyncTask;

import android.support.v4.util.LruCache;

import android.widget.ImageView;


@SuppressLint("NewApi")

public class HttpAsyncTask {


// 緩存

private LruCache<String, Bitmap> mCache;


public HttpAsyncTask() {


int maxMeory = (int) Runtime.getRuntime().maxMemory();

int cacheSize = maxMeory / 4;

mCache = new LruCache<String, Bitmap>(cacheSize) {

@Override

protected int sizeOf(String key, Bitmap value) {


return value.getByteCount();

}

};


}


public void addBitmap2Cache(String url, Bitmap bitmap) {


if (getBitmap4Cache(url) == null) {


mCache.put(url, bitmap);

}


}


public Bitmap getBitmap4Cache(String url) {


return mCache.get(url);

}


public void showImageByAsync(ImageView imageView, String url) {

Bitmap bitmap = getBitmap4Cache(url);

if (bitmap == null) {

new NewsAsyncTask(imageView, url).execute(url);

} else {

imageView.setImageBitmap(bitmap);

}


// new NewsAsyncTask(imageView,url).execute(url);


}


public Bitmap getBitmapFromUrl(String sturl) {

Bitmap bitmap = null;

InputStream inputStream = null;

try {

URL url = new URL(sturl);

HttpURLConnection connection = (HttpURLConnection) url

.openConnection();

inputStream = new BufferedInputStream(connection.getInputStream());

bitmap = BitmapFactory.decodeStream(inputStream);

connection.connect();

return bitmap;


} catch (MalformedURLException e) {


e.printStackTrace();

} catch (IOException e) {


e.printStackTrace();

} finally {

if (inputStream != null) {

try {

inputStream.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

return null;


}


private class NewsAsyncTask extends AsyncTask<String, Void, Bitmap> {


private ImageView mimageView;

private String mUrl;


public NewsAsyncTask(ImageView imageView, String url) {

mimageView = imageView;

mUrl = url;

}


@Override

protected Bitmap doInBackground(String... params) {

// TODO Auto-generated method stub

String url = params[0];

Bitmap bitmap = getBitmap4Cache(url);

if (bitmap != null) {

addBitmap2Cache(url, bitmap);

}

return bitmap;

// return getBitmapFromUrl(params[0]);

}


@Override

protected void onPostExecute(Bitmap result) {

// TODO Auto-generated method stub

super.onPostExecute(result);

if (mimageView.getTag().equals(mUrl)) {

mimageView.setImageBitmap(result);

}

}


}


}


adapter 中也設置了



正在回答

4 回答

public NewsAsyncTask(ImageView imageView, String url) {

mimageView = imageView;

mUrl = url;

}

改為:

public NewsAsyncTask(ImageView imageView, String url) {

this.mimageView = imageView;

this.mUrl = url;

}


0 回復 有任何疑惑可以回復我~

import android.support.v4.util.LruCach ? ->.import android.util.LruCache;

0 回復 有任何疑惑可以回復我~

你的doInBackground里寫錯了吧,應該是getBitmapFromUrl,你寫的是getBitmap4Cache

3 回復 有任何疑惑可以回復我~

打斷點debug一下吧,看看具體什么地方問題

0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消
Android必學-異步加載
  • 參與學習       50617    人
  • 解答問題       326    個

了解Android中的異步加載處理方法,這是面試問的最多的知識點

進入課程

我的程序有一個問題,就是在添加了LruCache后就不顯示圖片了

我要回答 關注問題
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號