2

BlackBerry 開発は初めてです。しかし、アンドロイドについては良いです。

サーバーからの画像をListFieldにロードしたい。

以下のコードのように実装しましたが、成功しませんでした:

   package mypackage;


public class TempScreen extends MainScreen implements ListFieldCallback{
    Bitmap[] images=null;
    private ListField mylist;
    private static Bitmap _bitmap;
    private ImageDownloader downloader;
    int size = 0;
    String[] urls={
            "http://www.kentnews.co.uk/polopoly_fs/damian_lewis_at_port_lympne_wild_animal_park_c_taf_1_1738362!image/2626063106.jpg_gen/derivatives/landscape_225/2626063106.jpg",
            "http://www.kentnews.co.uk/polopoly_fs/damian_lewis_at_port_lympne_wild_animal_park_c_taf_1_1738362!image/2626063106.jpg_gen/derivatives/landscape_225/2626063106.jpg",
            "http://www.kentnews.co.uk/polopoly_fs/damian_lewis_at_port_lympne_wild_animal_park_c_taf_1_1738362!image/2626063106.jpg_gen/derivatives/landscape_225/2626063106.jpg",
            "http://www.kentnews.co.uk/polopoly_fs/damian_lewis_at_port_lympne_wild_animal_park_c_taf_1_1738362!image/2626063106.jpg_gen/derivatives/landscape_225/2626063106.jpg"};
    public TempScreen()
    {


        images=new Bitmap[urls.length];
        size = urls.length;
        mylist = new ListField();
        mylist.setCallback(this);
        mylist.setSize(4);
        mylist.setRowHeight(getFont().getHeight() * 3);
        add(mylist);
        Thread downloader=new Thread(new ImageDownloader());
        downloader.start();

    }


    public void drawListRow(ListField listField, Graphics graphics, int index,
            int y, int width) {

        if(images[index]==null)
         {
             //Load placeholder image
            _bitmap = Bitmap.getBitmapResource("close_btn.png");// load some bitmap
            // of your choice
            // here
         }
         else
             //Load Bitmap
            _bitmap = images[index]; 

        graphics.drawText("row details", 100, y + 30);
        //graphics.drawBitmap(0, y, _bitmap.getWidth(), _bitmap.getHeight(),_bitmap, 0, 0);
        mylist.invalidate(index);
    }

    public class ImageDownloader implements Runnable
    {
        public void run()
        { 
            for(int i=0; i<size;i++)
            { 
                 if(images[i]==null) 
                 { 
                      images[i]=connectServerForImage(urls[i].toString());//replace downloadImage method to whatever method       you have to download the bitmap from url 
                      UiApplication.getUiApplication().invokeLater(new Runnable(){
                          public void run()
                          {
                              mylist.invalidate();
                          }
                      });
                  }
             }
        }
    }
    public Object get(ListField listField, int index) {
        // TODO Auto-generated method stub
        return null;
    }
    public int getPreferredWidth(ListField listField) {
        // TODO Auto-generated method stub
        return 0;
    }
    public int indexOfList(ListField listField, String prefix, int start) {
        // TODO Auto-generated method stub
        return 0;
    }
    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+";interface=wifi");
            httpConnection = (HttpConnection) Connector.open(url);
            rc = httpConnection.getResponseCode();
            // System.out.println("===============================");
            Dialog.alert("beore if condition");
            if (rc == HttpConnection.HTTP_OK) {

                System.out.println(" ============= IN FUNCTION. . . . .");
                httpInput = httpConnection.openInputStream();
                InputStream inp = httpInput;
                byte[] b = IOUtilities.streamToBytes(inp);
                EncodedImage hai = EncodedImage.createEncodedImage(b, 0,
                        b.length);
                bitmp = hai.getBitmap();
            } else {
                throw new IOException("HTTP response code: " + rc);
            }
        } 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;
    }
}

どこが間違っているのかわからない。同じことで私を助けてくれるバディをお願いします。

4

3 に答える 3

2

次のリンクを使用してみてください:http: //www.coderholic.com/blackberry-webbitmapfield/

上記のリンクで提案されているように、WebBitmapFieldという名前の別のクラスを作成する必要があります。

リストフィールドの画像オブジェクトでそのクラスを使用する方法:

  • すべての画像URLに対してWebBitmapFieldオブジェクトを作成します
  • photoList_vectorは、リストフィールドに要素を入力するためのベクトルです。

    WebBitmapField web = new WebBitmapField("http://www.image1.png"); 
    
    photoList_vector.addElement(web);
    
    web = new WebBitmapField("http://www.image2.png"); 
    
    photoList_vector.addElement(web);
    

次に、このベクトルを使用してリストフィールドで作業します......

上記の行では、画像を取得するために複数のリクエストを同時に送信するときに、各画像が特定のWebBitmapFieldオブジェクトに対応するようにしています。

次に、各オブジェクトがベクターに追加され、リストフィールドに追加できるようになります。

各URL送信は、WebBitmapFieldのオブジェクトに関連付けられています。

したがって、リクエストは別のスレッドで送信されますが、関連付けられたオブジェクトにのみ関連付けられます

それが役に立てば幸い :)

于 2012-12-18T06:16:36.443 に答える
2

私は以前にこの問題に取り組んでおり、ここに私の手法を投稿していますが、Screen クラスと非常に関連しているため、理想的な解決策ではありませんが、それでも役立つ可能性があります。

画面クラスの最初に、リスト フィールド項目と同じサイズのビットマップ用の配列を 1 つ用意します。

public class TempScreen extends MainScreen{
    Bitmap[] images=null;
    String[] urls={"image1_url", "image2_url".....};
    public TempScreen()
    {

        images=new Bitmap[urls.length];

    }

ListFieldCallBack の drawListRow メソッドで、次のことを確認します。

 public void drawListRow(ListField list, Graphics g, int index, int y, int width){
     if(bitmap[index]==null)
     {
         //Load placeholder image
     }
     else
         //Load Bitmap    
 }

次に、画像をダウンロードするためのスレッド クラスを作成します。

public class ImageDownloader implements Runnable
{
    public void run()
    { 
        for(int i=0; i<size;i++)
        { 
             if(images[i]==null) 
             { 
                  images[i]=downloadImage(url[i]);//replace downloadImage method to whatever method       you have to download the bitmap from url 
                  UiApplication.getUiApplication().invokeLater(new Runnable(){
                      public void run()
                      {
                          list.invalidate()
                      }
                  });
              }
         }
    }
}

screen クラスのコンストラクターで、コールバックを listfield に設定した後、スレッドを開始します。

Thread downloader=new Thread(new ImageDownloader());
downloader.start();

編集: TempScreen コンストラクターを次のように変更します。

public TempScreen()
    {

        images=new Bitmap[urls.length];
        size = urls.length;
        mylist = new ListField();
        mylist.setCallback(this);
        mylist.setSize(4);
        mylist.setRowHeight(getFont().getHeight() * 3);
        add(mylist);
        Thread downloader=new Thread(new ImageDownloader());
        downloader.start();
    }
于 2012-12-13T13:08:32.773 に答える