Search This Blog

Friday, 1 June 2012

How to get image from server and show it on Blackberry device

Hello friends,
                   No explanations , just copy and use this function to get the image from server and display.....

CODE:

 public static Bitmap connectServerForImage(String url) {
        HttpConnection httpConnection = null;
        DataOutputStream httpDataOutput = null;
        InputStream httpInput = null;
        int rc;
        String ConnParameter="";
        
        if(WLANInfo.getWLANState()==WLANInfo.WLAN_STATE_CONNECTED){
            ConnParameter = ";interface=wifi";
        }else if((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_DIRECT) == CoverageInfo.COVERAGE_DIRECT) {
            String carrierUid = getCarrierBIBSUid();
            if(carrierUid == null)
            {
                // Has carrier coverage, but not BIBS.  So use the carrier's TCP network
//                logMessage("No Uid");
                ConnParameter = ";deviceside=true";
            }
            else
            {
                // otherwise, use the Uid to construct a valid carrier BIBS request
//                logMessage("uid is: " + carrierUid);
                ConnParameter = ";deviceside=false;connectionUID="+carrierUid + ";ConnectionType=mds-public";
            }
       
           
        }else if((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_MDS) == CoverageInfo.COVERAGE_MDS)
        {
//            logMessage("MDS coverage found");
             ConnParameter = ";deviceside=false";
        }
       
        Bitmap bitmp = null;
        try {
            httpConnection = (HttpConnection) Connector.open(url+ConnParameter);
            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);
            int currentWidthFixed32 = Fixed32.toFP(hai.getWidth());
            int currentHeightFixed32 = Fixed32.toFP(hai.getHeight());
            int reqWidthStandard = Display.getWidth();
            int reqWidth = reqWidthStandard;
            int reqHeight = Display.getHeight()/2;
            int requiredWidthFixed32 = Fixed32.toFP(reqWidth);
            int requiredHeightFixed32 = Fixed32.toFP(reqHeight);
            int scaleXFixed32 = Fixed32.div(currentWidthFixed32, requiredWidthFixed32);
            int scaleYFixed32 = Fixed32.div(currentHeightFixed32, requiredHeightFixed32);
            hai = hai.scaleImage32(scaleXFixed32, scaleYFixed32);
            bitmp= 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;
            }

  

No comments:

Post a Comment