【问题标题】:Set a bitmap to NetworkImageView将位图设置为 NetworkImageView
【发布时间】:2015-05-12 12:25:18
【问题描述】:

我正在动态创建图像视图并显示它。但是我在滚动时遇到了重新加载问题,所以我决定使用 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):致命异常:线程 7325 05-12 19:11:13.971:E/AndroidRuntime(11177):进程:com.dar.app,PID:11177 05-12 19:11:13.971:E/AndroidRuntime(11177):java.lang.NegativeArraySizeException:-350 05-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): at com.android.volley.CacheDispatcher.run(CacheDispatcher.java:100)

【问题讨论】:

  • 我是安卓新手,至少请指导我
  • 请检查我更新的代码,我正在做的是动态创建它并传递给持有者。下面的代码我不明白
  • 它有效吗?如果是这样,请增加我的答案。谢谢
  • 不,我很困惑..如果我在滚动列表时使用 Imageview,它将再次重新下载图像并且图像位置正在改变。
  • 如果您不使用对象,是的。您可以做的是使用图像缓存。 (developer.android.com/training/displaying-bitmaps/…) 或在存储参考图像的对象列表中。

标签: android networkimageview


【解决方案1】:

你可以使用凌空图像下载器

很简单 默认 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));

【讨论】:

  • @Mister 如果此编辑的代码有效..那么我会更新我的答案..接受这个问题的答案...这对其他人来说很容易
  • R.android.defaultimg for android 我收到错误
  • 因为我在适配器中实现
  • 为默认和错误设置一些图像...并将其命名为我在代码上使用的名称...并将“R.android”更改为“R.drawable”..我也已经更新了代码
  • OMG 非常感谢 :) 但如果我滚动两次 3 次,它将关闭应用程序
【解决方案2】:

最终代码

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-04
    • 2017-05-19
    • 2016-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多