我如何在黑莓中显示URL中的图像?

时间:2012-02-18 05:50:38

标签: blackberry

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

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;

        }


    }

提前完成。

2 个答案:

答案 0 :(得分:4)

试试这个,但这里的网址扩展非常重要。

如果您的手机使用wifi然后&#34 ;; interface = wifi"这是工作否则它不会工作所以对于网址扩展请验证以下网址

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

答案 1 :(得分:1)

我正在解决这个问题。

查看所有连接的此网址: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;
       }
}