1

我已经制作了一个带有图像的列表字段,该列表字段是从在线 xml 文件中解析的。似乎将图像下载到列表字段中阻止了解析列表字段“文本”内容的过程,我想显示列表字段“文本”的内容,然后开始处理图像的下载,然后显示图片。
这是调用下载器类的代码:

        线程 t = 新线程();
    {
        字符串 imageFilename = imageurlStrin.substring(imageurlSrin.lastIndexOf('/') + 1);
        String saveDire = "file:///store/home/user/pictures/listfield/"+imageFilename;
        尝试 {
        FileConnection fconn = (FileConnection)Connector.open(saveDire);
        如果(fconn.exists()){
         // 没做什么
        }
        if (!fconn.exists()) {  
        UrlToImage 位 = 新 UrlToImage(imageurlStrin);
        pic = bit.getbitmap();
        }
        }catch (异常 ioe) {
        System.out.println("错误 18");
        }
    };
    t.start();


这是下载器类代码:

public class UrlToImage implements Runnable{
    String imageurlStrin=null;
    BitmapDowloadListener listener=null;
    public static Bitmap _bmap;
    private EncodedImage eih1;

    public void run() {

            UrlToImage bit = new UrlToImage(imageurlStrin);

    }


    public UrlToImage(String imageurlStrin)
    {
            HttpConnection connection = null;
            InputStream inputStream = null;
            EncodedImage bitmap;

            byte[] dataArray = null;
            //byte[] data1 = null;

            try
            {
            connection = (HttpConnection) Connector.open(imageurlStrin, Connector.READ, true);
            inputStream = connection.openInputStream();
            byte[] responseData = new byte[10000];
            int length = 0;
            StringBuffer 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();
            dataArray = result.getBytes();
            }
            catch (final Exception ex)
            { }

            finally
            {
            try
            {
            inputStream.close();
            inputStream = null;
            connection.close();
            connection = null;
            }
            catch(Exception e){}
            }

            bitmap = EncodedImage.createEncodedImage(dataArray, 0,dataArray.length);
            // this will scale your image acc. to your height and width of bitmapfield

            int multH;
            int multW;
            int currHeight = bitmap.getHeight();
            int currWidth = bitmap.getWidth();
            int scrhi = Display.getWidth()/4;
            int scrwe = Display.getWidth()/4;

            multH= Fixed32.div(Fixed32.toFP(currHeight),Fixed32.toFP(scrhi));//height
            multW = Fixed32.div(Fixed32.toFP(currWidth),Fixed32.toFP(scrwe));//width
            bitmap = bitmap.scaleImage32(multW,multH);

            Bitmap thefinal = bitmap.getBitmap();

            //url = StringUtils.replaceAll(url ,"http://u.bbstars.com/i-", "");
            final String imageFilename = imageurlStrin.substring(imageurlStrin.lastIndexOf('/') + 1);

            String saveDire = "file:///store/home/user/pictures/listfield/"+imageFilename;
            String Dire = "file:///store/home/user/pictures/listfield/";

            JPEGEncodedImage finalJPEG = JPEGEncodedImage.encode(thefinal, 100);

            byte[] raw_media_bytes = finalJPEG.getData();
            int raw_length = finalJPEG.getLength();
            int raw_offset = finalJPEG.getOffset();

            FileConnection filecon = null;
            OutputStream out = null;

            try {
                    filecon = (FileConnection) Connector.open(Dire,Connector.READ_WRITE);
                    if(!filecon.exists()){
                            filecon.mkdir();
                    }
                    filecon = (FileConnection) Connector.open(saveDire,Connector.READ_WRITE);
                if(!filecon.exists()){
                    filecon.create();
                 }
                    out = filecon.openOutputStream();
                    out.write(raw_media_bytes, raw_offset, raw_length);
                    out.close();
                    filecon.close();
                    System.out.println("----------------file saved"+imageFilename);

            } catch (IOException e) {
                System.out.println("---------------===================- error saving the file");
                };
                try {
                    FileConnection fconn = (FileConnection)Connector.open(saveDire);
                    if (fconn.exists()) {
                        InputStream input = fconn.openInputStream();
                        int available = input.available();
                        final byte[] data1=IOUtilities.streamToBytes(input);
                        input.read(data1, 0, available);
                        eih1 = EncodedImage.createEncodedImage(data1,0,data1.length);
                    }
                }catch (Exception ioe) {
                        System.out.println("error gettin bitmap details from the piture");
                    }  
            _bmap=eih1.getBitmap();
            }
            public Bitmap getbitmap()
            {
            return _bmap;
            }

}

我应该怎么做才能防止 UI 阻塞,我想要完美的为什么要调用该下载器类而不停止解析其他列表字段内容的过程?

4

1 回答 1

1

我认为您声明线程对象的方式可能只是一个简单的语法问题。 在此处查看有关 Thread 的 BlackBerry 文档

当您创建 aThread时,您通常使用Thread您自己的子类扩展该类,该子类实现该run()方法,或者您将一个新Runnable对象传递给您的对象的构造Thread函数。在您的代码中,您实际上声明了一个Thread实例并创建它,但不要给它一个Runnable覆盖默认run()方法。所以,这个线程不会在后台做任何事情。

您实际上已经在本地范围内声明了一段代码。如果您只是将一些代码放在一组没有附加到任何东西的大括号 ( {and ) 中,就会发生这种情况:}

Thread t = new Thread();
// this next curly bracket starts a "local scope".  it is NOT part of Thread t!
{
    String imageFilename = imageurlStrin.substring(imageurlStrin.lastIndexOf('/') + 1);
    // The rest of your code here will not be executed on Thread t.  It will be executed
    // on the thread where you instantiate Thread t, right before you call t.start();
    // If this code is called on the main/UI thread (which it probably is), then the 
    // network request will block the UI thread, which will stop the loading of the rest
    // of the list.
};
t.start();

你可能想要的是这样的:

Thread t = new Thread(new Runnable() {

    public void run() {

        String imageFilename = imageurlStrin.substring(imageurlStrin.lastIndexOf('/') + 1);
        String saveDire = "file:///store/home/user/pictures/listfield/"+imageFilename;
        try {
            FileConnection fconn = (FileConnection)Connector.open(saveDire);
            if (fconn.exists()) {
                // do nothing
            }
            if (!fconn.exists()) {  
                UrlToImage bit = new UrlToImage(imageurlStrin);
                pic = bit.getbitmap();
            }
        } catch (Exception ioe) {
            System.out.println("error 18");
        }
    }

});

t.start();    

试试看,看看是否能解决问题。

于 2012-09-15T22:35:56.677 回答