0

我使用了几种不同的方法从 Web 服务器下载图像,将其显示在图像视图中。我面临同样的问题,下载后图像在图像视图中显示为空白。我没有得到我错的地方。我正在使用模拟器。

这是我下载图片的代码

    private static InputStream OpenHttpConnection(String urlString) 
        throws IOException
        {
            InputStream in = null;
            int response = -1;

            URL url = new URL(urlString); 
            URLConnection conn = url.openConnection();

            if (!(conn instanceof HttpURLConnection))                     
                throw new IOException("Not an HTTP connection");

            try{
                HttpURLConnection httpConn = (HttpURLConnection) conn;
                httpConn.setAllowUserInteraction(false);
                httpConn.setInstanceFollowRedirects(true);
                httpConn.setRequestMethod("GET");
                httpConn.connect(); 

                response = httpConn.getResponseCode();                 
                if (response == HttpURLConnection.HTTP_OK) {
                    in = httpConn.getInputStream();                                 
                }                     
            }
            catch (Exception ex)
            {
                throw new IOException("Error connecting");            
            }
            return in;     
        }
        static Bitmap DownloadImage(String URL)
        {        
            Bitmap bitmap = null;
            InputStream in = null;        
            try {
                in = OpenHttpConnection(URL);
                bitmap = BitmapFactory.decodeStream(in);
                in.close();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            return bitmap;                
        }

这是我显示图像的代码

   private class LongOperation extends AsyncTask<String, Void, String> {



      @Override

      protected String doInBackground(String... params) {

        // perform long running operation operation

                Bitmap message_bitmap = null; 
                // Should we download the image?
                if ((image_url != null) && (!image_url.equals("")))            
                            {
                    message_bitmap = 
                         ImageDownloader.DownloadImage(image_url);

                }
                // If we didn't get the image, we're out of here
                if (message_bitmap == null) {

                    Log.d("Image", "Null hai");
                }

        return null;

      }





      @Override

      protected void onPostExecute(String result) {

        // execution of result of Long time consuming operation
          pDialog.dismiss();
          iv.setImageDrawable(message_bitmap);
          Log.d("Image", "Displayed");
      }



      /* (non-Javadoc)
               * @see android.os.AsyncTask#onPreExecute()

       */

      @Override

      protected void onPreExecute() {

      // Things to be done before execution of long running operation.
            pDialog = new ProgressDialog(CommonUtilities.this);
            pDialog.setMessage(Html.fromHtml("Please Wait..."));
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
      }







    }
4

1 回答 1

0

我使用此代码加载 img 表单 url

ImageView v_thumburl = (ImageView) rowView
                .findViewById(R.id.v_thumb_url);
        thumburl = temp.getString(temp.getColumnIndex("thumburl"));
        Drawable drawable = LoadImageFromWebOperations(thumburl);
        v_thumburl.setImageDrawable(drawable);

private Drawable LoadImageFromWebOperations(String url) {
    try {
        InputStream is = (InputStream) new URL(url).getContent();
        Drawable d = Drawable.createFromStream(is, "src name");
        return d;
    } catch (Exception e) {
        System.out.println("Exc=" + e);
        return null;
    }
}
于 2013-01-17T11:08:13.940 回答