gpt4 book ai didi

android - 将位图设置为 NetworkImageView

转载 作者:行者123 更新时间:2023-11-29 20:46:43 29 4
gpt4 key购买 nike

我正在动态创建 Imageview 并显示它。但是我在滚动时遇到了重新加载问题所以我决定使用 NetworkImageView 但在这里我无法设置位图。它没有显示任何错误,但没有显示任何图像。在我的代码下面...

final NetworkImageView imageView = new NetworkImageView(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(150,
200);
params.weight = 1;
//imageView.setImageBitmap(null);
imageView.setLayoutParams(params);
imageView.setMaxHeight(200);
imageView.setMaxWidth(150);
// bitmap =//
// BitmapFactory.decodeResource(context.getResources(),R.drawable.ic_menu_folder);
// Log.i("Bitmaps Counts", String.valueOf(pos));
/* comment all this...
new Thread() {
public void run() {


bitmap = downloadImage(tempValues.getItemAbsolutePath());


((Activity) context).runOnUiThread(new Runnable() {
@Override
public void run() {

imageView.setImageBitmap(bitmap);

}
});

}
}.start();*/
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
ImageLoader mImageLoader = new ImageLoader(requestQueue,new LruBitmapCache(1000000));
mImageLoader.get(tempValues.getItemAbsolutePath(),
ImageLoader.getImageListener(imageView,
R.android.defaultimg,
R.android.errorimg));

layout.addView(imageView);

下载图片代码..

/* unused method
private Bitmap downloadImage(String url) {
Bitmap bitmap = null;
InputStream stream = null;
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inSampleSize = 1;

try {
stream = getHttpConnection(url);
bitmap = BitmapFactory.decodeStream(stream, null, bmOptions);
stream.close();

} catch (IOException e1) {
e1.printStackTrace();
}
return bitmap;
}

private InputStream getHttpConnection(String urlString) throws IOException {
InputStream stream = null;
URL url = new URL(urlString);
URLConnection connection = url.openConnection();

try {
HttpURLConnection httpConnection = (HttpURLConnection) connection;
httpConnection.setRequestMethod("GET");
httpConnection.connect();

if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
stream = httpConnection.getInputStream();
}
} catch (Exception ex) {
ex.printStackTrace();
}
return stream;
}*/

错误日志

05-12 19:11:13.971: E/AndroidRuntime(11177): 致命异常: Thread-732505-12 19:11:13.971: E/AndroidRuntime(11177): 进程:com.dar.app,PID:1117705-12 19:11:13.971: E/AndroidRuntime(11177): java.lang.NegativeArraySizeException: -35005-12 19:11:13.971: E/AndroidRuntime (11177): 在 com.android.volley.toolbox.DiskBasedCache.streamToBytes (DiskBasedCache.java:316)05-12 19:11:13.971: E/AndroidRuntime(11177): 在 com.android.volley.toolbox.DiskBasedCache.get(DiskBasedCache.java:117)05-12 19:11:13.971: E/AndroidRuntime(11177): 在 com.android.volley.CacheDispatcher.run(CacheDispatcher.java:100)

最佳答案

you can use volley image downloder

很简单默认 img 已设置...加载程序请求图像 ..如果使用 url 找到图像,它将设置 .. 或者如果错误,它将设置错误图像 .. 这样你就会在加载 img 时通知错误

 RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
ImageLoader mImageLoader = new ImageLoader(requestQueue,new LruBitmapCache(1000000));
mImageLoader.get(<img-url>,
ImageLoader.getImageListener(<imageview-object>,
R.drawable.defaultimg,
R.drawable.errorimg));

关于android - 将位图设置为 NetworkImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30190823/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com