当前位置: 首页 > 面试题库 >

在Blackberry中显示URL中的图像

西门飞星
2023-03-14
问题内容

我使用以下代码从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);

但是图像无法在用户界面中预览。有没有一种特殊的方法可以从Blackberry中的URL预览图像。(我是说,应将图像放入一个临时数组中以预览图像吗?)谢谢


问题答案:

试试这个 -

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

// URLBitmapField类在下面给出-

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);

}


 类似资料:
  • 我有一个问题,不知道如何修复它。我有这样的消息: 这是我的信息:tongue_out::眨眼:dsdangrydsangryhoanghamobile.comdsd:眨眼:这是表情符号和更多的文字…… 它将像下面的图像显示:眨眼:,:舌头_出:和其他表情符号是从链接:https://filev4.subiz.com/wink.png.所有的图像是一个字符串,现在我必须从url和文本解析表情符号显示

  • 我想在GridView中显示图像列表。但我做不到。我想知道资产中图像列表的数据类型,以及构造函数在这种情况下的帮助。我刚刚开始学习颤振,我不知道如何使用它。

  • 我用两个jsp文件和一个控制器构建了一个小servlet。 这是: 这是控制器: 当我在登录jsp页面上输入用户名和密码并点击提交时,这两个属性都显示在URL行中。 这就是我得到的: 我该怎么解决?

  • 我想显示图像,但不知道该怎么做。我不知道是必须安装一些库文件,还是简单地安装就可以了。实际上我想做图像处理,但首先我必须接受图像输入并显示图像,然后我可以得到图像处理的效果作为输出,并决定它(算法)是否正确。我只安装了eclipse。我也在谷歌上搜索过,但是无论他们建议什么都不管用。要么我得装点什么,要么不装。 我已经尝试了以下代码: 它只是显示一个图形窗口,但不能显示图像“bishnu.jpg”

  • 问题内容: 我有点卡住。为什么不做这项工作?我只是收到一条错误消息: java.lang.NoSuchMethodError:主要 线程“主”中的异常 问题答案: main需要是静态的,并且必须具有String []而不是String的参数。 为了解决这个问题,将所有东西都放在构造函数中,例如

  • 我创建了一个动态web项目,以便通过Servlet显示JSP(我不能使用任何类似Spring的框架…)。默认情况下,我构建的类放在Build/classes中,因此在我将输出文件夹更改为WebContent/WEB-INF/classes后,我的应用程序运行良好。之后,我将该项目转换为Maven项目。(我不记得我上次使用servlet/JSP时需要更改输出文件夹,那是3年前!)。 无论如何,现在我