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







    } 

回答

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

试试这个代码我希望它可以帮助你ü

+0

我想下载SD卡 – user1918034

+0

任何错误是在日志中传来的图像,显​​示和存储 – Jagan

+0

它显示JPEG解码器结果:0 – user1918034

0

使用延迟加载Web Server的形象,并设置在ImageView的......从这个Example我希望这有助于你对当前的问题,以及未来... :-)