1

画像でリストフィールドを作成しました。リストフィールドはオンラインxmlファイルから解析されます。画像をリストフィールドにダウンロードすると、リストフィールド「テキスト」のコンテンツを解析するプロセスがブロックされているようです。リストフィールド「テキスト」のコンテンツを表示してから、画像のダウンロードの処理を開始してから、画像。
ダウンローダ クラスを呼び出すコードは次のとおりです。

        スレッド t = 新しいスレッド();
    {
        文字列 imageFilename = imageurlStrin.substring(imageurlStrin.lastIndexOf('/') + 1);
        String saveDire = "file:///store/home/user/pictures/listfield/"+imageFilename;
        試す {
        FileConnection fconn = (FileConnection)Connector.open(saveDire);
        if (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

スレッドオブジェクトを宣言する方法に単純な構文の問題があるかもしれないと思います。 こちらのスレッドに関するBlackBerryのドキュメントを参照してください

を作成するときはThread、通常、メソッドThreadを実装する独自のサブクラスでクラスを拡張するか、オブジェクトのコンストラクターにrun()新しいオブジェクトを渡します。コードでは、実際にインスタンスを宣言して作成しますが、インスタンスを指定したり、デフォルトのメソッドをオーバーライドしたりしないでください。したがって、このスレッドはバックグラウンドでは何もしません。RunnableThreadThreadRunnablerun()

基本的に、ローカルスコープ内でコードのチャンクを宣言しました。これは、何にも接続されていない中括弧({および)のセット内にコードを挿入した場合に発生します。}

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 に答える