将位图设置为NetworkImageView



我正在动态创建ImageViews并显示它。但是我在滚动时遇到了重新加载问题,因此我决定使用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;
    }*/

errorlog

05-12 19:11:13.971:e/androidruntime(11177):致命例外:线程-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.java:100)

您可以使用凌空图像下行

很容易默认IMG设置了... iage的加载程序请求。如果使用URL找到图像,它将设置..或如果错误将设置错误映像。

 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));

最终代码

public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
    private ImageView imageView;
    private Bitmap image;
    private LinearLayout linear;
    public DownloadImageTask(ImageView imageView, LinearLayout linear) {
       this.imageView = imageView;
       this.linear = linear;
    }
    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            image = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            image = null;
        }
        return image;
    }
    @SuppressLint("NewApi")
    protected void onPostExecute(Bitmap result) {
        if (result != null) {
            imageView.setImageBitmap(result);
            linear.addView(imageView);
        }
    }
}

现在打电话给您的代码:

final ImageView imageView = new ImageView(context);
final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(150, 200);
params.weight = 1;
imageView.setLayoutParams(params);
imageView.setMaxHeight(200);
imageView.setMaxWidth(150);
new DownloadImageTask(imageView, layout).execute(tempValues.getItemAbsolutePath());

相关内容

  • 没有找到相关文章

最新更新