Android 无法显示下载的图像
我以前曾致力于从sd卡中获取图像,并在列表视图中显示图像,该工作使用:Android 无法显示下载的图像,android,image,url,Android,Image,Url,我以前曾致力于从sd卡中获取图像,并在列表视图中显示图像,该工作使用: imgView.setImageURI(Uri.parse(ImagePath)); 现在,我尝试从URL显示图像,使用以下行,但图像未显示在列表视图中,以下是使用的行: imgView.setImageBitmap(getBitmapFromURL(ImagePath)); 其中,getBitmapFromURL为: public static Bitmap getBitmapFromURL(String src) {
imgView.setImageURI(Uri.parse(ImagePath));
现在,我尝试从URL显示图像,使用以下行,但图像未显示在列表视图中,以下是使用的行:
imgView.setImageBitmap(getBitmapFromURL(ImagePath));
其中,getBitmapFromURL为:
public static Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
}
catch (IOException e) {
e.printStackTrace();
return null;
}
}
没有异常显示,图像只是无法显示
需要紧急解决
谢谢,这是一个同步加载(我个人不会使用这个,因为如果要加载这么多图像,应用程序会有点滞后) 如果我是你,我会学习异步或惰性适配器 编辑 我忘了这些代码是从哪里来的(非常感谢您提供了一位出色的代码作者) 给你
public Bitmap getBitmap(String bitmapUrl) {
try {
URL url = new URL(bitmapUrl);
return BitmapFactory.decodeStream(url.openConnection().getInputStream());
}
catch(Exception ex) {return null;}
}
public enum BitmapManager {
INSTANCE;
private final Map<String, SoftReference<Bitmap>> cache;
private final ExecutorService pool;
private Map<ImageView, String> imageViews = Collections
.synchronizedMap(new WeakHashMap<ImageView, String>());
private Bitmap placeholder;
BitmapManager() {
cache = new HashMap<String, SoftReference<Bitmap>>();
pool = Executors.newFixedThreadPool(5);
}
public void setPlaceholder(Bitmap bmp) {
placeholder = bmp;
}
public Bitmap getBitmapFromCache(String url) {
if (cache.containsKey(url)) {
return cache.get(url).get();
}
return null;
}
public void queueJob(final String url, final ImageView imageView,
final int width, final int height) {
/* Create handler in UI thread. */
final Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
String tag = imageViews.get(imageView);
if (tag != null && tag.equals(url)) {
if (msg.obj != null) {
imageView.setImageBitmap((Bitmap) msg.obj);
} else {
imageView.setImageBitmap(placeholder);
Log.d(null, "fail " + url);
}
}
}
};
pool.submit(new Runnable() {
public void run() {
final Bitmap bmp = downloadBitmap(url, width, height);
Message message = Message.obtain();
message.obj = bmp;
Log.d(null, "Item downloaded: " + url);
handler.sendMessage(message);
}
});
}
public void loadBitmap(final String url, final ImageView imageView,
final int width, final int height) {
imageViews.put(imageView, url);
Bitmap bitmap = getBitmapFromCache(url);
// check in UI thread, so no concurrency issues
if (bitmap != null) {
Log.i("inh","Item loaded from cache: " + url);
imageView.setImageBitmap(bitmap);
} else {
imageView.setImageBitmap(placeholder);
queueJob(url, imageView, width, height);
}
}
private Bitmap downloadBitmap(String url, int width, int height) {
try {
Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(
url).getContent());
bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
Log.i("nandi2 ako", ""+bitmap);
cache.put(url, new SoftReference<Bitmap>(bitmap));
return bitmap;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
这是一个同步加载。(我个人不会使用这个原因,如果有这么多的图像要加载,应用程序有点滞后) 如果我是你,我会学习异步或惰性适配器 编辑 我忘了这些代码是从哪里来的(非常感谢您提供了一位出色的代码作者) 给你
public Bitmap getBitmap(String bitmapUrl) {
try {
URL url = new URL(bitmapUrl);
return BitmapFactory.decodeStream(url.openConnection().getInputStream());
}
catch(Exception ex) {return null;}
}
public enum BitmapManager {
INSTANCE;
private final Map<String, SoftReference<Bitmap>> cache;
private final ExecutorService pool;
private Map<ImageView, String> imageViews = Collections
.synchronizedMap(new WeakHashMap<ImageView, String>());
private Bitmap placeholder;
BitmapManager() {
cache = new HashMap<String, SoftReference<Bitmap>>();
pool = Executors.newFixedThreadPool(5);
}
public void setPlaceholder(Bitmap bmp) {
placeholder = bmp;
}
public Bitmap getBitmapFromCache(String url) {
if (cache.containsKey(url)) {
return cache.get(url).get();
}
return null;
}
public void queueJob(final String url, final ImageView imageView,
final int width, final int height) {
/* Create handler in UI thread. */
final Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
String tag = imageViews.get(imageView);
if (tag != null && tag.equals(url)) {
if (msg.obj != null) {
imageView.setImageBitmap((Bitmap) msg.obj);
} else {
imageView.setImageBitmap(placeholder);
Log.d(null, "fail " + url);
}
}
}
};
pool.submit(new Runnable() {
public void run() {
final Bitmap bmp = downloadBitmap(url, width, height);
Message message = Message.obtain();
message.obj = bmp;
Log.d(null, "Item downloaded: " + url);
handler.sendMessage(message);
}
});
}
public void loadBitmap(final String url, final ImageView imageView,
final int width, final int height) {
imageViews.put(imageView, url);
Bitmap bitmap = getBitmapFromCache(url);
// check in UI thread, so no concurrency issues
if (bitmap != null) {
Log.i("inh","Item loaded from cache: " + url);
imageView.setImageBitmap(bitmap);
} else {
imageView.setImageBitmap(placeholder);
queueJob(url, imageView, width, height);
}
}
private Bitmap downloadBitmap(String url, int width, int height) {
try {
Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(
url).getContent());
bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
Log.i("nandi2 ako", ""+bitmap);
cache.put(url, new SoftReference<Bitmap>(bitmap));
return bitmap;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
我以前遇到过这种问题,你可以参考,如果运气不好,试试我的代码
public static Bitmap loadImageFromUrl(String url) {
URL m;
InputStream i = null;
BufferedInputStream bis = null;
ByteArrayOutputStream out =null;
try {
m = new URL(url);
i = (InputStream) m.getContent();
bis = new BufferedInputStream(i,1024 * 8);
out = new ByteArrayOutputStream();
int len=0;
byte[] buffer = new byte[1024];
while((len = bis.read(buffer)) != -1){
out.write(buffer, 0, len);
}
out.close();
bis.close();
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
byte[] data = out.toByteArray();
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
return bitmap;
}
我以前遇到过这种问题,你可以参考,如果运气不好,试试我的代码
public static Bitmap loadImageFromUrl(String url) {
URL m;
InputStream i = null;
BufferedInputStream bis = null;
ByteArrayOutputStream out =null;
try {
m = new URL(url);
i = (InputStream) m.getContent();
bis = new BufferedInputStream(i,1024 * 8);
out = new ByteArrayOutputStream();
int len=0;
byte[] buffer = new byte[1024];
while((len = bis.read(buffer)) != -1){
out.write(buffer, 0, len);
}
out.close();
bis.close();
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
byte[] data = out.toByteArray();
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
return bitmap;
}
@AliRajput这是一个旧代码,据我记忆所及,有很多更复杂的代码和库在之后弹出thesse@AliRajputLog.d(空,“失败”+url);论公共职务(最终字符串url,最终图像视图ImageView,我认为最好使用调试器找出错误所在^ ^@AliRajput将url链接发送给我这似乎是问题所在..我认为它要么在SD卡上,要么在web上获得。如果您只使用本地域,我建议您使用Wampserver等来测试您的url。感谢代码,issue不是代码,而是正斜杠(我用反斜杠写错了地址)但是感谢代码需要惰性适配器或异步功能,因为图像的数量在以后会增加很多。@AliRajput这是一个旧代码,据我所记得的,有很多更复杂的代码和库在后面弹出thesse@AliRajputLog.d(null,“fail”+url);在公共void队列作业上(最终字符串url,最终图像视图ImageView,我认为最好使用调试器找出错误所在^ ^@AliRajput将url链接发送给我这似乎是问题所在..我认为它要么在SD卡上,要么在web上获得。如果您只使用本地域,我建议您使用Wampserver等来测试您的url。感谢代码,issue不是代码,而是正斜杠(我用反斜杠写错了地址)但是感谢代码需要惰性适配器或异步功能,因为图像的数量以后会增加很多..可能您的代码会正常工作,但我正在将图像显示到listview中,因此它无法正常工作;现在无法显示日志,因为我已经在使用Async作为图像数量调试另一个代码方面取得了很大进展可能会超出预期。无论如何,感谢您的代码可能会正常工作,但我正在将图像显示到listview中,因此它无法正常工作;现在无法显示日志,因为我已经在使用Async调试另一个代码方面取得了相当大的进展,因为图像的数量可能会超出预期。无论如何,感谢请参阅