我想创建一个活动,它将使用网址从Web加载图片,我想在列表视图中加载这些图片,我想在该图像前面显示该图像的一些特定文本和属性,如下图{ {3}}。有没有办法将这些图像临时存储在手机内存中。所以我该如何进行此活动请给我完美的解决方案,以便我可以完成我的应用程序。如何从网上加载图像。
答案 0 :(得分:0)
让我们试试这个
网址img_value =新网址(img [位置]);
Bitmap mIcon1 = BitmapFactory.decodeStream(img_value.openConnection().getInputStream());
holder.image.setImageBitmap(mIcon1);
此处holder.image是一个保存在xml图像视图中的图像小部件。
直接加载来自给定网址的图片。 或者通过创建名为DrawableManager的类来尝试如下所示...
new DrawableManager()。fetchDrawableOnThread(VAL3 [position],holder.icon);
package com.fsp.demo;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ImageView;
public class DrawableManager {
@SuppressWarnings("unchecked")
private final Map drawableMap;
public DrawableManager() {
drawableMap = new HashMap<String, SoftReference<Drawable>>();
}
@SuppressWarnings("unchecked")
public Drawable fetchDrawable(String urlString) {
SoftReference<Drawable> drawableRef = (SoftReference<Drawable>) drawableMap
.get(urlString);
if (drawableRef != null) {
Drawable drawable = drawableRef.get();
if (drawable != null)
return drawable;
// Reference has expired so remove the key from drawableMap
drawableMap.remove(urlString);
}
if (Constants.LOGGING)
Log.d(this.getClass().getSimpleName(), "image url:" + urlString);
try {
InputStream is = fetch(urlString);
Drawable drawable = Drawable.createFromStream(is, "src");
drawableRef = new SoftReference<Drawable>(drawable);
drawableMap.put(urlString, drawableRef);
if (Constants.LOGGING)
Log.d(this.getClass().getSimpleName(),
"got a thumbnail drawable: " + drawable.getBounds()
+ ", " + drawable.getIntrinsicHeight() + ","
+ drawable.getIntrinsicWidth() + ", "
+ drawable.getMinimumHeight() + ","
+ drawable.getMinimumWidth());
return drawableRef.get();
} catch (MalformedURLException e) {
if (Constants.LOGGING)
Log.e(this.getClass().getSimpleName(), "fetchDrawable failed",
e);
return null;
} catch (IOException e) {
if (Constants.LOGGING)
Log.e(this.getClass().getSimpleName(), "fetchDrawable failed",
e);
return null;
}
}
@SuppressWarnings("unchecked")
public void fetchDrawableOnThread(final String urlString,
final ImageView imageView) {
// Log.v("Drawable_url",urlString);
SoftReference<Drawable> drawableRef = (SoftReference<Drawable>) drawableMap
.get(urlString);
if (drawableRef != null) {
Drawable drawable = drawableRef.get();
if (drawable != null) {
imageView.setImageDrawable(drawableRef.get());
return;
}
// Reference has expired so remove the key from drawableMap
drawableMap.remove(urlString);
}
final Handler handler = new Handler() {
public void handleMessage(Message message) {
imageView.setImageDrawable((Drawable) message.obj);
}
};
Thread thread = new Thread() {
@Override
public void run() {
// TODO : set imageView to a "pending" image
Drawable drawable = fetchDrawable(urlString);
Message message = handler.obtainMessage(1, drawable);
handler.sendMessage(message);
}
};
thread.start();
}
private InputStream fetch(String urlString) throws MalformedURLException,
IOException {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet request = new HttpGet(urlString);
HttpResponse response = httpClient.execute(request);
return response.getEntity().getContent();
}
}