【问题标题】:How i can display images from URL in blackberry?如何在黑莓中显示来自 URL 的图像?
【发布时间】:2012-03-27 19:04:38
【问题描述】:

我有简单的图像 url,我想在位图字段中显示该图像。 这是我的课..但它不会给出任何结果。

public class UrlToImage
{

    public static Bitmap _bmap;
    UrlToImage(String url)
    {
        HttpConnection connection = null; 
        InputStream inputStream = null; 
        EncodedImage bitmap;
        byte[] dataArray = null;

    try 
    { 
        connection = (HttpConnection) Connector.open(url, Connector.READ, true); 
        inputStream = connection.openInputStream(); 
        byte[] responseData = new byte[10000]; 
        int length = 0; 
        StringBuffer rawResponse = new StringBuffer(); 
        while (-1 != (length = inputStream.read(responseData))) 
        { 
        rawResponse.append(new String(responseData, 0, length)); 
        } 
        int responseCode = connection.getResponseCode(); 
        if (responseCode != HttpConnection.HTTP_OK) 
        { 
        throw new IOException("HTTP response code: " 
        + responseCode); 
        } 

        final String result = rawResponse.toString(); 
        dataArray = result.getBytes(); 
    } 
    catch (final Exception ex) 
    { }

    finally 
    { 
        try 
        { 
        inputStream.close(); 
        inputStream = null; 
        connection.close(); 
        connection = null; 
        } 
        catch(Exception e){} 
        } 

        bitmap = EncodedImage.createEncodedImage(dataArray, 0,dataArray.length);
        // this will scale your image acc. to your height and width of bitmapfield

        int multH;
        int multW;
        int currHeight = bitmap.getHeight();
        int currWidth = bitmap.getWidth();
        multH= Fixed32.div(Fixed32.toFP(currHeight),Fixed32.toFP(480));//height
        multW = Fixed32.div(Fixed32.toFP(currWidth),Fixed32.toFP(360));//width
        bitmap = bitmap.scaleImage32(multW,multH);

        _bmap=bitmap.getBitmap();
        }
        public Bitmap getbitmap()
        {
        return _bmap;

        }


    }

提前感谢。

【问题讨论】:

    标签: blackberry


    【解决方案1】:

    试试这个,但这里的 url 扩展很重要。

    如果您的手机使用 wifi,则“;interface=wifi”这可以正常工作,否则它将无法正常工作,因此对于 url 扩展,请验证以下 url

    For url extensions Link

     public static Bitmap connectServerForImage(String url)
    {
         HttpConnection httpConnection = null;
         DataOutputStream httpDataOutput = null;
         InputStream httpInput = null;
         int rc;
         Bitmap bitmp = null;
         try 
         {
                  httpConnection = (HttpConnection) Connector.open(url+";interface=wifi");
                  rc = httpConnection.getResponseCode();
                  if (rc == HttpConnection.HTTP_OK) 
                  {
                       httpInput = httpConnection.openInputStream();
                       InputStream inp = httpInput;
                       byte[] b = IOUtilities.streamToBytes(inp);
                       EncodedImage hai = EncodedImage.createEncodedImage(b, 0, b.length);
                       bitmp=hai.getBitmap();
                   }else{
                        throw new IOException("HTTP response code: " + rc);
                   }
          }catch (Exception ex) {
               System.out.println("URL Bitmap Error........" + ex.getMessage());
          } finally 
          {
           try
           {
                    if (httpInput != null)
                     httpInput.close();
                    if (httpDataOutput != null)
                     httpDataOutput.close();
                    if (httpConnection != null)
                     httpConnection.close();
    
           } catch (Exception e) 
           {
               e.printStackTrace();
           }
          }
        return bitmp;
    }
    

    【讨论】:

      【解决方案2】:

      我正在解决这个问题..

      查看所有连接的网址:BlackBerry simulator can connect to web service, but real device can't

      public final class MyScreen extends MainScreen
      {
          String url="";
          public MyScreen()
          {        
      
              setTitle("MyTitle");
      
              BitmapField pic = new BitmapField(connectServerForImage(url)); 
              this.add(pic);
          }
      
          public static Bitmap connectServerForImage(String url) 
          {
      
              HttpConnection httpConnection = null;
              DataOutputStream httpDataOutput = null;
              InputStream httpInput = null;
              int rc;
      
              Bitmap bitmp = null;
              try 
              {
              if (WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED) 
                  {
                      httpConnection = (HttpConnection) Connector.open(url+ ";interface=wifi",Connector.READ_WRITE, true);  
                  }
                  else 
                  {
                      httpConnection = (HttpConnection) Connector.open(url+";deviceside=true", Connector.READ_WRITE, true);
                  } 
               rc = httpConnection.getResponseCode();
               if (rc != HttpConnection.HTTP_OK) {
                throw new IOException("HTTP response code: " + rc);
               }
               httpInput = httpConnection.openInputStream();
               InputStream inp = httpInput;
               byte[] b = IOUtilities.streamToBytes(inp);
               EncodedImage hai = EncodedImage.createEncodedImage(b, 0, b.length);
               return hai.getBitmap();
      
              } catch (Exception ex) {
               System.out.println("URL Bitmap Error........" + ex.getMessage());
              } finally {
               try {
                if (httpInput != null)
                 httpInput.close();
                if (httpDataOutput != null)
                 httpDataOutput.close();
                if (httpConnection != null)
                 httpConnection.close();
               } catch (Exception e) {
                e.printStackTrace();
      
               }
              }
              return bitmp;
             }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-09-09
        • 1970-01-01
        • 2021-06-26
        • 2020-06-25
        • 1970-01-01
        • 2017-01-07
        • 1970-01-01
        相关资源
        最近更新 更多