开发者

how can I construct BitmapImage in black berry

开发者 https://www.devze.com 2023-02-26 10:06 出处:网络
I want to construct a BitmapImage from url in bl开发者_如何学JAVAack berry. How can I do this?Try to use the following code :

I want to construct a BitmapImage from url in bl开发者_如何学JAVAack berry. How can I do this?


Try to use the following code :

Bitmap img=Bitmap.createBitmapFromBytes((getImageFromUrl(url)).getBytes()), 0,-1, 1);

where getImageFromUrl(String url) is a method to fetch image from url.

public static String getImageFromUrl(String url) {
            //Image img = null;
            String imageData = null;
            try
            {            
              imageData = getDataFromUrl(url);
              //img = Image.createImage(imageData.getBytes(), 0,imageData.length() );          
            }
            catch(Exception e1) {
                e1.printStackTrace();
            }

            return imageData;
        }

        public static String getDataFromUrl(String url) 
              throws IOException {

            StringBuffer b = new StringBuffer();
            InputStream is = null;
            HttpConnection c = null;

            long len = 0 ;
            int ch = 0;

            ConnectionFactory connFact = new ConnectionFactory();
              ConnectionDescriptor connDesc;
              connDesc = connFact.getConnection(url);

              if (connDesc != null)
              {
                  //HttpConnection httpConn;
                  c = (HttpConnection)connDesc.getConnection();
              }

           // c = (HttpConnection)Connector.open(url);
            is = c.openInputStream();
            len = c.getLength();
            if( len != -1) {
                // Read exactly Content-Length bytes
                for(int i =0 ; i < len ; i++ )
                    if((ch = is.read()) != -1) {
                    b.append((char) ch);
                    }
            } else {
                //Read until the connection is closed.
                while ((ch = is.read()) != -1) {
                    len = is.available() ;
                    b.append((char)ch);
                }
            }

            is.close();
            c.close();
            return b.toString();
        }  

Hope this will solve your problem...:)

0

精彩评论

暂无评论...
验证码 换一张
取 消