为什么我的图像没有下载到ImageViews?

时间:2014-07-25 07:30:32

标签: java android json

我对原生Android开发很陌生。我的应用目前正在做什么:

  1. 从我们的服务器下载JSON图片网址

  2. 为每张图片ImageView添加ListView

  3. 我已经获得了JSON,现在正在使用ImageAdapter(扩展BaseAdapter)来填充ListView,但我还是遇到错误:

    • 我在println needs a message函数中创建InputStream期间获得OpenHttpGETConnection

    这是我的代码(按从高到低的顺序删除了不必要的代码):

    1. 加载JSON后,我的onPostExecute代码将运行以启动适配器:

      private class DownloadImagesTextTask extends AsyncTask<String, Void, String> {
          protected String doInBackground(String... urls){
              return getImages(urls[0]);
          }
      
          @Override
          protected  void onPostExecute(String result){
              JSONArray images = null;
              images = new JSONArray(result);
      
              // TURN images into array of imageviews
              ListView listView = (ListView) findViewById(R.id.list);
      @here---->   listView.setAdapter(new ImageAdapter(getBaseContext(), images));
              Log.d("DownloadTextTask", images.toString());
          }
      }
      
    2. 这是我的ImageAdapter代码:

      public class ImageAdapter extends BaseAdapter {
          private Context context;
          private JSONArray images;
          public ImageAdapter(Context c, JSONArray i){
              context = c;
              images = i;
          }
          // returns an imageview view
          public View getView(int position, View convertView, ViewGroup parent){
              ImageView imageView = new ImageView(context);
              try {
                  String url = "http://www.example.com/" + images.get(position);
      @here---->  imageView.setImageBitmap(getImageBitmap(url));
              } catch (Exception e){
                  Log.d("LoadImage", e.getLocalizedMessage());
              }
              imageView.setScaleType(ImageView.ScaleType.FIT_XY);
      
              return imageView;
          }
      }
      
    3. 这是我的getImageBitmap功能:

      private Bitmap getImageBitmap(String url){
          Bitmap bitmap = null;
          InputStream in = null;
          try {
      
      @here---->  in = OpenHttpGETConnection(url);
      
          } catch (Exception e) { 
      
      @i_get
      @this ---->  Log.wtf("OpenGET", e.getMessage() + ": " + url ); 
      @error
      
          }
          bitmap = BitmapFactory.decodeStream(in);
          in.close();
      
          return bitmap;
      }
      
    4. 最后,这是OpenHttpGETConnection函数:

      public static InputStream OpenHttpGETConnection(String url){
          InputStream inputStream = null;
          HttpClient httpClient = null;
          HttpResponse httpResponse = null;
      
      
          httpClient = new DefaultHttpClient();
          httpResponse = httpClient.execute(new HttpGet(url));
          inputStream = httpResponse.getEntity().getContent();
      
          return inputStream;
      }
      
    5. 这里有相关的LogCat行:(通过JSON文件传来3张图片)

      A/OpenGET﹕ println needs a message: http://www.example.com/image1.jpg
      
    6. 奇怪的是,我在加载JSON数据时使用相同的OpenHttpGETConnection,因此我非常确定它会返回正确的InputStream对象。将它用于文本与二进制数据(jpg)时是否有一些注意事项?

      提前致谢!

1 个答案:

答案 0 :(得分:1)

首先,在适配器中,在getView()方法中,您不会回收这些项目。看一下ViewHolder模式(http://www.javacodegeeks.com/2013/09/android-viewholder-pattern-example.html)。 然后,getImageBitmap()似乎在同一个线程上执行,我想知道为什么它不会崩溃。我想你在那里下载实际图像。它应该异步完成,你应该发送ImageView的引用,下载完成后你应该加载它。当然,如果您将bitmap放在ImageView内,屏幕上有好ImageView(因为它可能已被回收),您必须要关心。 要摆脱所有这些问题,您可以使用Picasso libraryhttp://square.github.io/picasso/),这将为您完成所有这些。