2012-02-18 3 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; 

     } 


    } 

고지.

답변

4

시도해주세요.하지만 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

내가 얻고 솔루션 URL을 다음 확인 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; 
     } 
} 
관련 문제