本文介绍了从URL中黑莓显示图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的code至preVIEW从URL的图像。

I used following code to preview an image from an url.

Bitmap bannerImage=Bitmap.getBitmapResource("http://www.asianmirror.lk/english/images/stories/demo/hot_news/top_news/sanga1_latest.jpg");
BitmapField banner=new BitmapField(bannerImage);
add(banner);

但图像没有在UI preVIEW。是否有在黑莓的URL特殊的方式preVIEW图像。(我是指,要我把图像中的一个临时数组preVIEW形象?)谢谢

But the image doesn't preview in the UI. Is there is special way to preview images from a url in blackberry.(I means, shall I put the Image in to a temporary Array to preview the Image?) Thank you

推荐答案

试试这个 -

URLBitmapField wmf= new  util.URLBitmapField("http://www.asianmirror.lk/english/images/stories/demo/hot_news/top_news/sanga1_latest.jpg")
add(wmf);

下面// URLBitmapField类给出 -

//URLBitmapField class is given below-

public class URLBitmapField extends BitmapField implements URLDataCallback {
EncodedImage result=null ;
public static Bitmap myImage;

public static EncodedImage _encoded_img=null ;

int _imgWidth = 140;
int _imgHeight = 140;
int _imgMargin = 10;

public URLBitmapField(String url) {
    try {
        http_image_data_extrator.getWebData(url, this);
    }
    catch (Exception e) {}
}

public Bitmap getBitmap() {
    if (_encoded_img == null) return null;
    return _encoded_img.getBitmap();
}

public void callback(final String data) {
    if (data.startsWith("Exception")) return;

    try {
        byte[] dataArray = data.getBytes();

        //bitmap = EncodedImage.createEncodedImage(dataArray, 0, dataArray.length);//no scale

        _encoded_img = EncodedImage.createEncodedImage(dataArray, 0, dataArray.length); // with scale
        _encoded_img = sizeImage(_encoded_img, _imgWidth, _imgHeight);

    constants.image=_encoded_img;

        //myImage=cropImage(_encoded_img.getBitmap());

        setImage(_encoded_img);
        UiApplication.getUiApplication().getActiveScreen().invalidate();
    }
    catch (final Exception e){}
}

public EncodedImage sizeImage(EncodedImage image, int width, int height) {


      int currentWidthFixed32 = Fixed32.toFP(image.getWidth());
      int currentHeightFixed32 = Fixed32.toFP(image.getHeight());

      int requiredWidthFixed32 = Fixed32.toFP(width);
      int requiredHeightFixed32 = Fixed32.toFP(height);

      int scaleXFixed32 = Fixed32.div(currentWidthFixed32,
        requiredWidthFixed32);
      int scaleYFixed32 = Fixed32.div(currentHeightFixed32,
        requiredHeightFixed32);

      result = image.scaleImage32(scaleXFixed32, scaleYFixed32);
      return result;
}


public class http_image_data_extrator {
    static String url_="";
    static StringBuffer rawResponse=null;
    //static String result = null;
         public static void getWebData(String url, final URLDataCallback callback) throws IOException {
             //url_=url;

                 HttpConnection connection = null;
                 InputStream inputStream = null;

                try {


                    if ((WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED)
                            && RadioInfo
                                    .areWAFsSupported(RadioInfo.WAF_WLAN)) {
                        url += ";interface=wifi";
                    }

                    connection = (HttpConnection) Connector.open(url, Connector.READ, true);

                    String location=connection.getHeaderField("location");

                    if(location!=null){


                        if ((WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED)
                                && RadioInfo
                                        .areWAFsSupported(RadioInfo.WAF_WLAN)) {
                            location += ";interface=wifi";
                        }


                        connection = (HttpConnection) Connector.open(location, Connector.READ, true);

                    }else{
                        connection = (HttpConnection) Connector.open(url, Connector.READ, true);
                    }


                    inputStream = connection.openInputStream();
                    byte[] responseData = new byte[10000];
                    int length = 0;
                    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();
                    UiApplication.getUiApplication().invokeAndWait(new Runnable() {
                        public void run(){

                            callback.callback(result);


                        }
                    });
                }
                catch (final Exception ex) {
                    UiApplication.getUiApplication().invokeLater(new Runnable() {
                        public void run() {
                            callback.callback("Exception (" + ex.getClass() + "): " + ex.getMessage());
                        }
                    });
                }
    }

}



public interface URLDataCallback {

    public void callback(String data);

}

这篇关于从URL中黑莓显示图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 04:46
查看更多