异步加载图片在模拟器上可以下载显示,但在真机上却不行?
代码如下:public class AsyncImageLoader2
{
private HashMap<String, SoftReference<Bitmap>> imageCache;
private static AsyncImageLoader2 asyncImageLoader;
public AsyncImageLoader2() {
imageCache = new HashMap<String, SoftReference<Bitmap>>();
}
/**
* @return 得到类实例
*/
public static AsyncImageLoader2 getInstance() {
if (asyncImageLoader == null) {
asyncImageLoader = new AsyncImageLoader2();
}
return asyncImageLoader;
}
/**
* @param imageUrl
* 图片url
* @param imageCallback
* 回调函数
* @return 图片
*/
public Bitmap loadDrawable(final String imageUrl,
final ImageCallback imageCallback) {
if (imageCache.containsKey(imageUrl)) {
SoftReference<Bitmap> softReference = imageCache.get(imageUrl);
Bitmap bitmap = softReference.get();
if (bitmap != null) {
return bitmap;
}
}
final Handler handler = new Handler() {
public void handleMessage(Message message) {
imageCallback.imageLoaded((Bitmap) message.obj, imageUrl);
}
};
new Thread() {
@Override
public void run() {
Bitmap bitmap = loadImageFromUrl(imageUrl);
imageCache.put(imageUrl, new SoftReference<Bitmap>(bitmap));
Message message = handler.obtainMessage(0, bitmap);
handler.sendMessage(message);
}
}.start();
return null;
}
/**
* @param url
* 图片url
* @return 下载到的图片
*/
public static Bitmap loadImageFromUrl(String url) {
URL m;
InputStream i = null;
try {
m = new URL(url);
HttpURLConnection conn = (HttpURLConnection) m.openConnection();
conn.setConnectTimeout(30 * 1000);
conn.setRequestMethod("GET");
i = conn.getInputStream();
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Bitmap bitmap = BitmapFactory.decodeStream(i);
// Drawable d = Drawable.createFromStream(i, "src");
return bitmap;
}
public inte易做图ce ImageCallback {
public void imageLoaded(Bitmap imageBitmap, String imageUrl);
}
}
调用:
loadImage2("http://218.6.6.42/MagazineFiles/NewsPic/2011120848008166.jpg", R.id.testsyncdownloadimage1);
loadImage2("http://218.6.6.42/MagazineFiles/NewsPic/2011120847325490.jpg", R.id.testsyncdownloadimage2);
loadImage2("http://218.6.6.42/MagazineFiles/NewsPic/2011120846554918.jpg", R.id.testsyncdownloadimage3);
loadImage2("http://218.6.6.42/MagazineFiles/NewsPic/2011120829306415.jpg", R.id.testsyncdownloadimage4);
private AsyncImageLoader2 asyncImageLoader2 = new AsyncImageLoader2();
private void loadImage2(final String url,final int id)
{
Bitmap cacheImage = asyncImageLoader2.loadDrawable(url, new AsyncImageLoader2.ImageCallback()
{
@Override
public void imageLoaded(Bitmap imageBitmap, String imageUrl)
{
((ImageView)findViewById(id)).setImageBitmap(imageBitmap);
}
});
if(cacheImage != null)
{
((ImageView)findViewById(id)).setImageBitmap(cacheImage);
}
}
xml文件如下:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/testsyncdownloadimage1"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/testsyncdownloadimage2"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/testsyncdownloadimage3"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/testsyncdownloadimage4"
/>
</LinearLayout>
</ScrollView>
补充:移动开发 , Android