位图图像不能从URL链接显示黑莓 [英] Bitmap Image not displaying from a URL link Blackberry

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

问题描述

出于某种原因,我的黑莓应用程序崩溃每当我试图从使用互联网的URL显示位图图像。

我找到了 downloadImage()功能很容易理解,相比于其他计算器上跟随。其他人没有必须就如何落实其功能的例子。我已经测试了功能downloadImage很多次,都失败了。

请给人以code为例说明。谢谢你。

总之,编译器在这一点止于此:

g.drawBitmap(10,Y + 6,50,50,imageBmp,0,0);

下面是整个code:

包parsepack;进口java.io.ByteArrayOutputStream中;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.util.Vector中;进口javax.microedition.io.Connector;
进口javax.microedition.io.HttpConnection;
进口javax.microedition.io.StreamConnection;进口net.rim.device.api.system.Bitmap;
进口net.rim.device.api.system.DeviceInfo;
进口net.rim.device.api.system.Display;
进口net.rim.device.api.system.En codeDIMAGE;
进口net.rim.device.api.ui.DrawStyle;
进口net.rim.device.api.ui.Field;
进口net.rim.device.api.ui.FieldChangeListener;
进口net.rim.device.api.ui.Graphics;
进口net.rim.device.api.ui.Manager;
进口net.rim.device.api.ui.UiApplication;
进口net.rim.device.api.ui.component.BitmapField;
进口net.rim.device.api.ui.component.Dialog;
进口net.rim.device.api.ui.component.LabelField;
进口net.rim.device.api.ui.component.ListField;
进口net.rim.device.api.ui.component.ListFieldCallback;
进口net.rim.device.api.ui.container.MainScreen;
进口net.rim.device.api.ui.container.VerticalFieldManager;
进口net.rim.device.api.xml.parsers.DocumentBuilder;
进口net.rim.device.api.xml.parsers.DocumentBuilderFactory;进口org.w3c.dom.Document中;
进口org.w3c.dom.Node中;
进口org.w3c.dom.NodeList;公共类xmlparsing扩展UIApplication的实现ListFieldCallback,FieldChangeListener
{    公共静态无效的主要(字串[] args)抛出IOException异常
    {
    xmlparsing应用=新xmlparsing();
    app.enterEventDispatcher();
    }
众长mycolor;
连接_connectionthread;
私有静态ListField _list;
私有静态矢量listElements =新的向量();
私有静态矢量listPrice =新的向量();
私有静态矢量listAbstract =新的向量();
私有静态矢量listIcon =新的向量();
私人矢量listInfoVector =新的向量();
公共MainScreen屏幕=新MainScreen();
位图imageBmp = NULL;
VerticalFieldManager mainManager;
VerticalFieldManager subManager;
UIApplication的UI = UiApplication.getUiApplication();
公共xmlparsing()抛出IOException异常
{
    超();
    pushScreen(屏);    最后的位图BackgroundBitmap指令= Bitmap.getBitma $ P $的PSource(blackbackground.png);     mainManager =新VerticalFieldManager(Manager.NO_VERTICAL_SCROLL | Manager.NO_VERTICAL_SCROLLBAR)
        {         公共无效漆(图形图像)
          {
            graphics.drawBitmap(0,0,Display.getWidth(),Display.getHeight(),BackgroundBitmap指令,0,0);            super.paint(图形);
          }        };        subManager =新VerticalFieldManager(Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR)
        {
            保护无效sublayout(INT了maxWidth,诠释了maxHeight)
            {
                INT displayWidth = Display.getWidth();
                INT displayHeight = Display.getHeight();                super.sublayout(displayWidth,displayHeight);
                setExtent(displayWidth,displayHeight);
            }
        };
    screen.add(mainManager);    _list =新ListField()
    {
        公共无效漆(图形图像)
        {
            graphics.setColor((INT)mycolor);
            super.paint(图形);
        }
        保护布尔navigationClick(INT的地位,诠释时间)
        {
            尝试
            {
                //此处导航到另一个屏幕
                ui.pushScreen(新ResultScreen());
            }
            赶上(例外五)
            {
                的System.out.println(异常 - :navigationClick()+ e.toString());
            }
            返回true;
        }
    };
    mycolor =至0x00FFFFFF;
        _list.invalidate();
        _list.setEmptyString(*订阅不可*,DrawStyle.HCENTER);
    _list.setRowHeight(70);
    _list.setCallback(本);
    mainManager.add(subManager);
   listElements.removeAllElements();
   listPrice.removeAllElements();
   listAbstract.removeAllElements();
   listIcon.removeAllElements();
   _connectionthread =新的Connection();
   _connectionthread.start();
 }
私有类连接扩展Thread
{
    公共连接()
    {
       超();
    }    公共无效的run(){
        文档文档;
        康涅狄格州的StreamConnection = NULL;
        InputStream为= NULL;
        尝试{            康恩=(的StreamConnection) Connector.open(\"http://imforchange.org/international-movement-for-change/testing/data.xml\"+\";deviceside=true\");            的DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            docBuilderFactory.setIgnoringElementContentWhitespace(真);
            docBuilderFactory.setCoalescing(真);
            的DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            docBuilder.isValidating();
            是= conn.openInputStream();
            DOC = docBuilder.parse(是);
            doc.getDocumentElement()正常化()。
            节点列表list1的= doc.getElementsByTagName(eventName的);
                的for(int i = 0; I< list1.getLength();我++){
                节点textNode = list1.item(ⅰ).getFirstChild();
                listElements.addElement(textNode.getNodeValue());                }
                节点列表列表2 = doc.getElementsByTagName(eventPrice);
                的for(int i = 0; I< list2.getLength();我++){
                节点textNode = list2.item(ⅰ).getFirstChild();
                listPrice.addElement(textNode.getNodeValue());                }                节点列表项目list3 = doc.getElementsByTagName(eventAbstract);
                的for(int i = 0; I< list3.getLength();我++){
                节点textNode = list3.item(ⅰ).getFirstChild();
                listAbstract.addElement(textNode.getNodeValue());                }                节点列表list4 = doc.getElementsByTagName(eventIcon);
                的for(int i = 0; I< list4.getLength();我++){
                节点textNode = list4.item(ⅰ).getFirstChild();
                listIcon.addElement(textNode.getNodeValue());                }                }赶上(例外五){
                    的System.out.println(e.toString());
                    } {最后
                        如果(是!= NULL){
                        尝试{is.close();
                        }赶上(IOException异常忽略){}
                        }如果(参数conn!= NULL){
                            尝试{conn.close(); }
                            赶上(IOException异常忽略){}
                            }} UiApplication.getUiApplication()的invokeLater(Runnable的新(){
                                公共无效的run(){
                                _list.setSize(listElements.size());
                                subManager.add(_list);
                                screen.invalidate();
                                }
                                });
                            }}
公共无效drawListRow(ListField列表,图形克,INT指数,诠释Y,INT W)
{
    字符串文本=(字符串)listElements.elementAt(指数);
    串价格=(字符串)listPrice.elementAt(指数);
    字符串textAbstract =(字符串)listAbstract.elementAt(指数);
    INT yPos = 0 + Y;
    g.drawLine(0,yPos,W,yPos);
    g.drawText(文字,5,15 + Y,0,w)的;
    g.drawText($+价,5,15 + Y,DrawStyle.RIGHT,W-7);
    g.drawText(textAbstract,5,40 + Y,0,w)的;    //图像显示
    字符串图片网址=(字符串)listIcon.elementAt(指数);
    imageBmp = downloadImage(图片网址);
    g.drawBitmap(10,Y + 6,50,50,imageBmp,0,0);
}公共对象获取(ListField名单,INT指数)
{
    返回listElements.elementAt(索引);
}
公众诠释indexOfList(ListField列表,字符串preFIX,INT字符串)
{
    返回listElements.indexOf(preFIX,字符串);
}
公众诠释的get preferredWidth(ListField名单)
{
 返回Display.getWidth();
}/ *关于关于插入(警告),因为你不使用它的任何地方只是。
 *它看起来就像你还说,法允许xmlparsing类外code到
 *能够将项目插入列表。也许这就是你想要的,但你只是
 *还没有编写其他code使用该方法。我看你至少几个选择有:
 * /
公共无效插入(字符串toInsert,INT指数){
    listElements.addElement(toInsert);
    }公共无效fieldChanged(场场,诠释上下文){
}
公共静态位图downloadImage(字符串URL)
{
InputStream中的IStream = NULL;
恩codeDIMAGE位图;
    的HttpConnection的HttpConnection = NULL;        尝试
        {
                    的HttpConnection =(HttpConnection的)Connector.open(URL,Connector.READ_WRITE);            httpConnection.setRequestMethod(HttpConnection.GET);
            INT响应code = httpConnection.getResponse code();
            如果(响应code == HttpConnection.HTTP_OK){
                的IStream = httpConnection.openInputStream();
                ByteArrayOutputStream byteArrayOutputStream =新ByteArrayOutputStream();                字节[]缓冲区=新的字节[256];
                INT LEN = 0,IMAGESIZE = 0;                而(-1!=(LEN = iStream.read(缓冲液))){
                    byteArrayOutputStream.write(缓冲液);
                    IMAGESIZE + = LEN;
                }                byteArrayOutputStream.flush();
                字节[]为imageData = byteArrayOutputStream.toByteArray();
                byteArrayOutputStream.close();
                位= EN codedImage.createEn codeDIMAGE(为imageData,0,IMAGESIZE);
                BMP位图= bitmap.getBitmap();返回BMP;
            }
            }
            赶上(例外五)
            {
            }
返回null;
}
}


解决方案

如果您想从WEBURL显示黑莓手机上的画面中的图像。首先,你需要获得一个位图refrence。

您可以检查低于code:

在code将输入作为WEBURL,它会返回一个位图的参考。

 进口java.io.DataOutputStream中;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口javax.microedition.io.Connector;
进口javax.microedition.io.HttpConnection;
进口net.rim.device.api.io.IOUtilities;
进口net.rim.device.api.system.Bitmap;
进口net.rim.device.api.system.En codeDIMAGE;        公共类的getImage {
        公共静态位图connectServerForImage(字符串URL){
        的HttpConnection的HttpConnection = NULL;
        DataOutputStream类httpDataOutput = NULL;
        InputStream的httpInput = NULL;
        INT RC;
        位图bitmp = NULL;
        尝试{
        的HttpConnection =(HttpConnection的)Connector.open(URL);
        RC = httpConnection.getResponse code();
        如果(RC!= HttpConnection.HTTP_OK){
        抛出新IOException异常(HTTP响应code:+ RC);
        }
        httpInput = httpConnection.openInputStream();
        InputStream的INP = httpInput;
        字节[] B = IOUtilities.streamToBytes(INP);
        恩codeDIMAGE海= EN codedImage.createEn codeDIMAGE(B,0,b.length个);
        返回hai.getBitmap();
        }赶上(例外前){
        //的System.out.println(URL位图错误........+ ex.getMessage());
        } {最后
        尝试{
        如果(httpInput!= NULL)
        httpInput.close();
        如果(httpDataOutput!= NULL)
        httpDataOutput.close();
        如果(HttpConnection的!= NULL)
        httpConnection.close();
        }赶上(例外五){
        e.printStackTrace();
        }
        }
        返回bitmp;
        }
          }

最后,如果你想显示在BlackBerry屏幕上的图像,可以使用下面的方法

  g.drawBitmap(XPOS,ypos,W,H,图像,0,0); //通过位图参照这里

For some reason my blackberry application crashes whenever i try to display a bitmap image from a URL using the internet.

I found the downloadImage() function very easy to understand and to follow compared to others on stackoverflow. The others didnt have any examples on how to implement their function. I have have tested the function downloadImage many times and all failed.

Please give explanation with code example. Thanks.

Anyway, The compiler stops at this point here:

g.drawBitmap(10, y + 6, 50, 50, imageBmp, 0, 0);

Here is the entire code:

package parsepack;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.io.StreamConnection;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.DeviceInfo;
import net.rim.device.api.system.Display;
import net.rim.device.api.system.EncodedImage;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.ListFieldCallback;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.xml.parsers.DocumentBuilder;
import net.rim.device.api.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;



public class xmlparsing extends UiApplication implements ListFieldCallback, FieldChangeListener 
{

    public static void main(String[] args) throws IOException
    {
    xmlparsing app = new xmlparsing();
    app.enterEventDispatcher();
    }


public long mycolor ;
Connection _connectionthread;     
private static ListField _list;
private static Vector listElements = new Vector();
private static Vector listPrice = new Vector();
private static Vector listAbstract = new Vector();
private static Vector listIcon = new Vector();
private Vector listInfoVector = new Vector();
public MainScreen screen = new MainScreen();
Bitmap imageBmp = null;
VerticalFieldManager mainManager;
VerticalFieldManager subManager;
UiApplication ui = UiApplication.getUiApplication(); 


public  xmlparsing() throws IOException 
{ 
    super();
    pushScreen(screen);

    final Bitmap backgroundBitmap = Bitmap.getBitmapResource("blackbackground.png");

     mainManager = new VerticalFieldManager(Manager.NO_VERTICAL_SCROLL | Manager.NO_VERTICAL_SCROLLBAR )
        {

         public void paint(Graphics graphics)
          {
            graphics.drawBitmap(0, 0, Display.getWidth(),Display.getHeight(),backgroundBitmap, 0, 0);

            super.paint(graphics);
          }

        };

        subManager = new VerticalFieldManager(Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR )
        {
            protected void sublayout( int maxWidth, int maxHeight )
            {
                int displayWidth = Display.getWidth();
                int displayHeight = Display.getHeight();

                super.sublayout( displayWidth, displayHeight);
                setExtent( displayWidth, displayHeight);
            }
        };        


    screen.add(mainManager);





    _list = new ListField()
    {
        public void paint(Graphics graphics)
        {
            graphics.setColor((int) mycolor);
            super.paint(graphics);
        }
        protected boolean navigationClick(int status, int time)
        {
            try
            {
                //navigate here to another screen
                ui.pushScreen(new ResultScreen());
            }
            catch(Exception e)
            {
                System.out.println("Exception:-  : navigationClick() "+e.toString());
            }
            return true;
        }
    }; 
    mycolor = 0x00FFFFFF;
        _list.invalidate();
        _list.setEmptyString("* Feeds Not Available *", DrawStyle.HCENTER);
    _list.setRowHeight(70);
    _list.setCallback(this);
    mainManager.add(subManager);    
   listElements.removeAllElements();
   listPrice.removeAllElements();
   listAbstract.removeAllElements();
   listIcon.removeAllElements();
   _connectionthread = new Connection(); 
   _connectionthread.start();
 }


private class Connection extends Thread
{ 
    public Connection()
    { 
       super(); 
    } 

    public void run() {  
        Document doc;  
        StreamConnection conn = null; 
        InputStream is = null;  
        try {           

            conn = (StreamConnection) Connector.open("http://imforchange.org/international-movement-for-change/testing/data.xml"+";deviceside=true");           

            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();  
            docBuilderFactory.setIgnoringElementContentWhitespace(true);
            docBuilderFactory.setCoalescing(true);
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();    
            docBuilder.isValidating();      
            is = conn.openInputStream();    
            doc = docBuilder.parse(is);     
            doc.getDocumentElement().normalize();   
            NodeList list1 = doc.getElementsByTagName("eventName"); 
                for (int i = 0; i < list1.getLength(); i++) {    
                Node textNode = list1.item(i).getFirstChild();  
                listElements.addElement(textNode.getNodeValue());

                } 
                NodeList list2 = doc.getElementsByTagName("eventPrice"); 
                for (int i = 0; i < list2.getLength(); i++) {    
                Node textNode = list2.item(i).getFirstChild();  
                listPrice.addElement(textNode.getNodeValue());

                } 

                NodeList list3 = doc.getElementsByTagName("eventAbstract"); 
                for (int i = 0; i < list3.getLength(); i++) {    
                Node textNode = list3.item(i).getFirstChild();  
                listAbstract.addElement(textNode.getNodeValue());

                }

                NodeList list4 = doc.getElementsByTagName("eventIcon"); 
                for (int i = 0; i < list4.getLength(); i++) {    
                Node textNode = list4.item(i).getFirstChild();  
                listIcon.addElement(textNode.getNodeValue());

                }



                } catch (Exception e) {   
                    System.out.println(e.toString());                     
                    } finally {   
                        if (is != null) {  
                        try { is.close(); 
                        } catch (IOException ignored) {}   
                        }        if (conn != null) {  
                            try { conn.close(); } 
                            catch (IOException ignored) {} 
                            }    } UiApplication.getUiApplication().invokeLater(new Runnable() { 
                                public void run() {         
                                _list.setSize(listElements.size()); 
                                subManager.add(_list);
                                screen.invalidate();
                                }   
                                });
                            }





}




public void drawListRow(ListField list, Graphics g, int index, int y, int w) 
{  


    String text = (String)listElements.elementAt(index);
    String price = (String)listPrice.elementAt(index);
    String textAbstract = (String)listAbstract.elementAt(index);
    int yPos = 0+y;
    g.drawLine(0, yPos, w, yPos);
    g.drawText(text, 5, 15+y, 0, w);
    g.drawText("$"+price, 5, 15+y, DrawStyle.RIGHT, w-7);
    g.drawText(textAbstract, 5, 40+y, 0, w);



    // image to display
    String imageUrl = (String)listIcon.elementAt(index);
    imageBmp = downloadImage(imageUrl);
    g.drawBitmap(10, y + 6, 50, 50, imageBmp, 0, 0);
}



public Object get(ListField list, int index) 
{
    return listElements.elementAt(index); 
} 
public int indexOfList(ListField list, String prefix, int string) 
{ 
    return listElements.indexOf(prefix, string); 
} 
public int getPreferredWidth(ListField list) 
{ 
 return Display.getWidth(); 
}

/*Regarding the warning about insert(), that's just because you're not using it anywhere. 
 * It look like you've added that method to allow code outside the xmlparsing class to 
 * be able to insert items into the list. Maybe that's what you want, but you just 
 * haven't yet written the other code to use that method. I see you having at least a few choices:
 */
public void insert(String toInsert, int index) {
    listElements.addElement(toInsert);
    }

public void fieldChanged(Field field, int context) {


}


public static Bitmap downloadImage(String url)
{
InputStream iStream = null;
EncodedImage bitmap;
    HttpConnection httpConnection = null;

        try
        {
                    httpConnection = (HttpConnection) Connector.open(url, Connector.READ_WRITE);

            httpConnection.setRequestMethod(HttpConnection.GET);
            int responseCode = httpConnection.getResponseCode();
            if (responseCode == HttpConnection.HTTP_OK) {


                iStream = httpConnection.openInputStream();
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

                byte[] buffer = new byte[256];
                int len = 0, imageSize = 0;

                while (-1 != (len = iStream.read(buffer))) {
                    byteArrayOutputStream.write(buffer);
                    imageSize += len;
                }

                byteArrayOutputStream.flush();
                byte[] imageData = byteArrayOutputStream.toByteArray();
                byteArrayOutputStream.close();
                bitmap = EncodedImage.createEncodedImage(imageData, 0, imageSize);                                      
                Bitmap bmp = bitmap.getBitmap();

return bmp;
            }
            }
            catch (Exception e)
            {                      
            }
return null;                  
}




}

解决方案

If you want to Display an Image on BlackBerry Screen from a WebURL. First you need to get a BitMap refrence.

You can Check the below Code:

The Code will take input as WebURL and It will return a Bitmap reference.

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import net.rim.device.api.io.IOUtilities;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.EncodedImage;

        public class GetImage {
        public static Bitmap connectServerForImage(String url) {
        HttpConnection httpConnection = null;
        DataOutputStream httpDataOutput = null;
        InputStream httpInput = null;
        int rc;
        Bitmap bitmp = null;
        try {
        httpConnection = (HttpConnection) Connector.open(url);
        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);
        return 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;
        }
          }

And Finally,If you want to Display the Image on BlackBerry Screen, use the below method

g.drawBitmap(xpos, ypos, w, h, image, 0, 0);//pass the Bitmap reference Here

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆