2012-06-29 80 views
0

像我不能從URL加載圖像並播放到我listfield無法加載從URL

ImageLoader

public class Util_ImageLoader { 
    public static Bitmap getImageFromUrl(String url) { 
     Bitmap bitmap = null; 

     try { 
      String bitmapData = getDataFromUrl(url); 
      bitmap = Bitmap.createBitmapFromBytes(bitmapData.getBytes(), 0, 
        bitmapData.length(), 1); 
     } catch (Exception e1) { 
      e1.printStackTrace(); 
     } 

     return bitmap; 
    } 

    private static String getDataFromUrl(String url) { 
     StringBuffer b = new StringBuffer(); 
     InputStream is = null; 
     HttpConnection c = null; 

     long len = 0; 
     int ch = 0; 

     try { 
      c = (HttpConnection) Connector.open(url); 

      is = c.openInputStream(); 
      len = c.getLength(); 
      if (len != -1) { 
       for (int i = 0; i < len; i++) 
        if ((ch = is.read()) != -1) { 
         b.append((char) ch); 
        } 
      } else { 
       while ((ch = is.read()) != -1) { 
        len = is.available(); 
        b.append((char) ch); 
       } 
      } 

      is.close(); 
      c.close(); 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     return b.toString(); 
    } 
} 

ListField

image = new BitmapField(Util_ImageLoader.getImageFromUrl(
    "http://www.orientaldaily.com.my/images/articles/4_APRIL_BLACK_copy.jpg"), 
    Field.FIELD_HCENTER | Field.FIELD_VCENTER); 
row.add(image); 
field = getField(3); 
layoutChild(field, 100, 80); 
setPositionChild(field, getPreferredWidth() - 105, 5); 
+0

您使用的是調試器嗎?任何例外? (OS 5.0+,6.0+等)的哪種最低版本的BlackBerry OS? – Nate

+0

如何使用調試器?因爲嘗試捕獲已經看不到異常,我正在使用7.0 SDK和最低1.0,但我想將其更改爲5。0 SDK如果不是我的設備無法安裝 –

+0

您是否使用BlackBerry JDE構建軟件?或Eclipse與BlackBerry插件?或者是其他東西? – Nate

回答

0

您需要添加連接擴展到你的網址。

一樣,如果無線然後「;接口= WiFi」 的

Example: c = (HttpConnection) Connector.open(url+";interface=wifi"); 

使用perameter

c = (HttpConnection) Connector.open(url+getConnParam()); 

代碼示例完美連接用於獲取連接延長:

public static String getConnParam(){ 
     String connectionParameters = ""; 
     if (WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED) { 
     // Connected to a WiFi access point 
     connectionParameters = ";interface=wifi"; 
     } else { 
     int coverageStatus = CoverageInfo.getCoverageStatus(); 
     ServiceRecord record = getWAP2ServiceRecord(); 
     if (record != null 
     && (coverageStatus & CoverageInfo.COVERAGE_DIRECT) == 
     CoverageInfo.COVERAGE_DIRECT) { 
     // Have network coverage and a WAP 2.0 service book record 
     connectionParameters = ";deviceside=true;ConnectionUID=" 
     + record.getUid(); 
     } else if ((coverageStatus & CoverageInfo.COVERAGE_MDS) == 
     CoverageInfo.COVERAGE_MDS) { 
     // Have an MDS service book and network coverage 
     connectionParameters = ";deviceside=false"; 
     } else if ((coverageStatus & CoverageInfo.COVERAGE_DIRECT) == 
     CoverageInfo.COVERAGE_DIRECT) { 
     // Have network coverage but no WAP 2.0 service book record 
     connectionParameters = ";deviceside=true"; 
     } 

    } 
     return connectionParameters; 
    } 

參考網址:

http://www.blackberry.com/developers/docs/4.6.0api/javax/microedition/io/Connector.html#http

http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800451/800563/What_Is_-_Different_ways_to_make_an_HTTP_or_socket_connection.html?nodeid=826935&vernum=0

+0

'getWAP2ServiceRecord()'是指什麼? –

1

我相信艾倫說,他只需要支持OS 5.0及以上。如果這是真的,那麼我會不是建議他麻煩建設連接擴展字符串,如";interface=wifi"

OS 5.0增加了ConnectionFactory class,這使得這更容易。

BlackBerry設備可以使用多種不同網絡之一發出網絡請求傳輸。一些應用程序關心使用哪些傳輸。其他應用程序只需要任何可用運輸。

對於使用ConnectionFactory的一例create a Connection with the first available transport, see this example

對於更高級的例子,即示出了使用ConnectionFactory指定list of transports to use first, and which to not use at all, see this example

在第二個示例中,代碼使用的是Alan未使用的BrowserField。但是,他可以頂替他的代碼

c = (HttpConnection) Connector.open(url); 

有了這個

c = (HttpConnection) MyConnectionFactory.getConnection(url).getConnection(); 

其中MyConnectionFactoryshown in the sample code

+0

我仍然會使用'; interface = wifi',因爲現在仍然使用jre 7.0,但是,很快我可能會更改爲5.0,但我無法更改它,儘管我下載了jre 5.0 –

+0

@AlanLai,您不需要使用'; interface =任何在OS 5.0 **或更高版本**上運行的代碼均爲「wifi」。因此,對於OS 7.0,您絕對不需要繼續使用連接後綴字符串。 'ConnectionFactory'是現在推薦的方法。 – Nate

+0

問題是,如果我沒有使用,然後不能顯示,但應用後,它可以顯示 –