2012-02-18 18 views
2

私は単純な画像のURLを持っている、私はその画像をビットマップフィールドに表示したい。 これは私のクラスですが、結果は出ません。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; 

     } 


    } 

Thanx。

答えて

4

これを試してください。ただし、URLの拡張が重要です。

お使いの携帯はその後、無線LANを使用する場合は、「; =無線LANインターフェースは、」これは、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; 
} 
+0

ありがとうございます!!!!!! 3時間を過ごした後、私はあなたの助けを借りて作業しています:) – yanike

1

次検証pleseは..

は、すべての接続のために次のURLを参照してください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; 
     } 
} 
関連する問題